Ajp Prac 22
Ajp Prac 22
22
1) Write a program to send username and server will send the length of the
username to client.
i) Html file:
<!DOCTYPE html>
<html>
<body>
</form>
</body>
</html>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
@ WebServlet("/findlength")
String u1=req.getParameter("t1");
int l=u1.length();
PrintWriter pw=res.getWriter();
pw.close();
Output:
2) Write a servlet program to retrieve data from list and radio button using
HTML forms.
i) Html file:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="orange">Orange</option>
<option value="grape">Grape</option>
</select>
<br><br>
<label>Payment Method:</label><br>
</form>
</body></html>
ii) Servlet file:
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
@WebServlet("/list_radio_Servlett")
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
String s1 = req.getParameter("fruit");
String s2 = req.getParameter("payment");
pw.println("<h2>Your Preferences:</h2>");
pw.close();
}
iii) Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-
app_4_0.xsd"
version="4.0">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>list_radio_Servlet</servlet-name>
<servlet-class>list_radio_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>list_radio_Servlet</servlet-name>
<url-pattern>/list_radio_Servlet</url-pattern>
</servlet-mapping>
</web-app>
Output :
3) Develop a program to receive a student subject marks through Html forms TextField
and send the response as passed or failed in Exam.
i) Html file:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</form>
</body>
</html>
ii) Servlet File:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</form>
</body>
</html>
iii) Web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>MarksServlet</servlet-name>
<servlet-class>MarksServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MarksServlet</servlet-name>
<url-pattern>/MarksServlet</url-pattern>
</servlet-mapping>
</web-app>
Output:
4) Write the output of the following code considering below HTML is front end and
servlet as back end.
i) Html file:
<!DOCTYPE html>
<html>
<head>
<title>Authentication checking</title>
</head>
<body>
</form>
</body>
</html>
ii) Servlet file:
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
@WebServlet("/AuthenticateServlet")
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
String username,password;
username=req.getParameter("username");
password=req.getParameter("password");
if(username.equals("Abhishek")&& password.equals("abhishek12345"))
pw.println("Login succesfully");
else
pw.println("Login Unsuccessful");
pw.close();
}
iii) Web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>AuthenticateServlet</servlet-name>
<servlet-class>AuthenticateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AuthenticateServlet</servlet-name>
<url-pattern>/AuthenticateServlet</url-pattern>
</servlet-mapping>
</web-app>
Output: