0% found this document useful (0 votes)
129 views11 pages

Java Servlet Registration Guide

1. The document describes creating a web application using Servlets that accepts user registration data through a form and processes it. 2. It then describes creating a Servlet that reads and displays all the header information from an HTTP request. 3. Finally, it describes creating a form to generate cookies for user name and gender, and then retrieving those cookies on another page using a second Servlet.

Uploaded by

Nafis Siddiqui
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views11 pages

Java Servlet Registration Guide

1. The document describes creating a web application using Servlets that accepts user registration data through a form and processes it. 2. It then describes creating a Servlet that reads and displays all the header information from an HTTP request. 3. Finally, it describes creating a form to generate cookies for user name and gender, and then retrieving those cookies on another page using a second Servlet.

Uploaded by

Nafis Siddiqui
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Servlet

Aim1: Create a web application to accept values through a registration form and process the data
using Servlet
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<div id="1"><center><font size="5" color="blue">Registration Form</font></center> </div>
<br>

<form action="Register" method="post" >


<table border="1" bgcolor="lightgrey" align="center">
<tr align="center"><td>First Name </td><td><input type ="text" name="fn"></td></tr>
<tr align="center"><td>Middle name </td><td><input type="text" name="mn"></td></tr>
<tr align="center"><td>Last name </td><td><input type="text" name ="ln"></td></tr>
<tr align="center"><td>Password </td><td><input type ="password" name="pass"></td></tr>
<tr align="center"><td>Confirm password </td><td><input type ="password"
name="pass1"></td></tr>
<tr align="center"><td>Address </td><td><textarea name="add" ></textarea> </td></tr>
<tr align="center"><td>Mail ID </td><td><input type="text" name ="mail"></td></tr>
<tr align="center"><td>Ph no. </td><td><input type ="text" name="ph"></td></tr>
<tr align="center"><td>Country </td>
<td><select name="contry">
<option>India</option>
<option>USA</option>
<option>Australia</option>
</select>
</td>
</tr>
<tr align="center"><td>State
</td>
<td><select name="state">
<option>Maharashtra</option>
<option>Washington</option>
<option>Sydney</option>
</select>
</td>
</tr>
<tr align="center"><td>Gender
</td>
<td>
<input type="radio" name="r1" checked value="Male">Male
<input type="radio" name="r1" value="Female">Female
</td>
</tr>

<tr align="center"><td>Languages known </td>


<td>
<input type="checkbox" name="cb1" value="Marathi">Marathi
<input type="checkbox" name="cb2" value="Hindi">Hindi
<input type="checkbox" name="cb3" value="English">English
</td>
</tr>
<tr align="center"><td>Other Specification </td><td><textarea name="specfn" ></textarea>
</td></tr>
<tr align="center"><td><input type="submit" value="Submit" name="submit"></td><td>
<input type="reset" name="reset" ></td></tr>
</table>
</form>
</body>
</html>
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link] [Link]
<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>[Link]</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/Register</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
[Link]
package RegistrationPackage;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class Register extends HttpServlet {


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
[Link]("<font size=6 color=magenta>User Information</font><br>");
[Link]("<font size=4 color=blue>");
[Link]("First name : "+[Link]("fn"));
[Link]("<br>");
[Link]("Middle name : "+[Link]("mn"));
[Link]("<br>");
[Link]("Last name : "+[Link]("ln"));
[Link]("<br>");
[Link]("Address : "+[Link]("add"));
[Link]("<br>");
[Link]("E-Mail ID : "+[Link]("mail"));
[Link]("<br>");
[Link]("Phone No. : "+[Link]("ph"));
[Link]("<br>");
[Link]("Country : "+[Link]("contry"));
[Link]("<br>");
[Link]("State
: "+[Link]("state"));
[Link]("<br>");
[Link]("Gender : "+[Link]("r1"));
[Link]("<br>");
String s1,s2,s3,s4;
s4="Languages Known : ";
s1=[Link]("cb1");
if(s1!=null)
{
s4=s4+s1;
}
s4=s4+" ";
s2=[Link]("cb2");
if(s2!=null)
{
s4=s4+s2;
}
s4=s4+" ";
s3=[Link]("cb3");
if(s3!=null)
{
s4=s4+s3;
}
[Link](s4);
[Link]("<br>");
[Link]("Any other specification : "+[Link]("specfn"));
[Link]("</font>");

[Link]("<br>");
}
}

Output:-

Aim2 : Create a java Servlet to read all the headers from Servlet
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Request Headers</title>
</head>
<body>

<form action="HeaderServlet" >


<center>
<font color="grey" size="5">Click here to see headers</font> <br><br>
<input type="submit" value="Show Request Header" name="submit">
</center>
</form>
</body>
</html>
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link] [Link]
<servlet>
<servlet-name>HeaderServlet</servlet-name>
<servlet-class>[Link]</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HeaderServlet</servlet-name>
<url-pattern>/HeaderServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
HeaderServlet .java
package Headerpackage;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class HeaderServlet extends HttpServlet {
@Override

doGet

protected void
(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet Headers</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<center>");
[Link]("<h2>Request Headers</h2>");
[Link]("<table border=1 height=200 width=500><tr><td>Header</td><td>Value</td></tr>"
);
[Link]("<tr><td>Content Type : </td><td>"+[Link]()

+"</td>");
[Link]("<tr><td>Content Length :
</td><td>"+[Link]()+"</td>");
[Link]("<tr><td>Method : </td><td>"+[Link]()
+"</td>");
[Link]("<tr><td>Request URI :
</td><td>"+[Link]()+"</td>");
[Link]("<tr><td>Protocol : </td><td>"+[Link]()
+"</td>");
[Link]("<tr><td>Authentication Type :
</td><td>"+[Link]()+"</td>");
[Link]("<tr><td>Remote User :
</td><td>"+[Link]()+"</td>");
[Link]("</table>");
[Link]("</center>");
[Link]("</body>" );
[Link]("</html>");
}
}
Output

Aim3 : Create form to generate the cookies for name and gender and retrieve cookies
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Servlet Cookies</title>
</head>
<body>
<form action="First_Servlet" method="post">
<center>
<h2> User Details </h2>
First Name : <input type="text" name="t1"> <br><br>
Last Name : <input type="text" name="t2"> <br><br>
Gender : <input type="radio" name="r" checked="" value="male">Male <input type="radio"
name="r" value="female">Female <br><br>
<input type="submit" name="submit" value="Submit">
</center>
</form>
</body>
</html>
First_servlet.java

package cookies;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link].*;
import [Link].*;
public class First_Servlet extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
Cookie c1=new Cookie("Fname",[Link]("t1"));
Cookie c2=new Cookie("lname",[Link]("t2"));
Cookie c3=new Cookie("gen",[Link]("r"));
[Link](60*60*24);
[Link](60*60*24);
[Link](60*60*24);
[Link](c1);
[Link](c2);
[Link](c3);
[Link]("Cookie has been set ");
[Link]("<html>");
[Link]("<body>");
[Link]("<form action=Second_Servlet method=post>");
[Link]("<input type=submit value=Retrive Cookie name=submit");
[Link]("</form>");
[Link]("</body>");
[Link]("</html>");
}
}

Second_servlet.java
package cookies;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link].*;
import [Link].*;
public class Second_Servlet extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
Cookie c1=null,c2=null,c3=null;
Cookie[] cookies=null;
cookies=[Link]();
[Link]("<html>");
[Link]("<body>");
if(cookies!=null)
{
c1=cookies[0];
c2=cookies[1];
c3=cookies[2];
[Link]("Cookie has been retrived<br>");
if(([Link]()).equals("male") )
{
[Link]("Welcome Mr. "+[Link]()+" "+[Link]());
}
else
{
[Link]("Welcome Ms. "+[Link]()+" "+[Link]());
}
}
else
{
[Link]("No cookie found");
}
[Link]("</html>");
[Link]("</body>");
}
}

[Link]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link] [Link]
<servlet>
<servlet-name>First_Servlet</servlet-name>
<servlet-class>cookies.First_Servlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Second_Servlet</servlet-name>
<servlet-class>cookies.Second_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>First_Servlet</servlet-name>
<url-pattern>/First_Servlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Second_Servlet</servlet-name>
<url-pattern>/Second_Servlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Output:

You might also like