Java Servlet Program to Dispay user preferences.
i)HTML program to enter the information of employee.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Employee Registration Form</h1>
<form action="Servlet1" method="POST">
<h4>Name:<input type="text" name="u1"/></h4>
<h4>Age:<input type="text" name="a1"/></h4>
<h4>Salary:<input type="text" name="s1"/></h4>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
ii)Servlet1 accept the user information and creates and stores the information.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Servlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session=request.getSession();
String u=request.getParameter("u1");
String a=request.getParameter("a1");
String s=request.getParameter("s1");
session.setAttribute("name",u);
session.setAttribute("age",a);
session.setAttribute("sal",s);
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Servlet1</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Employee"+" "+u+"Registered Sucessfully " + "</h1>");
out.print("<a href='Servlet2'>view</a>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
}
iii) Servelet2 dispalay the information of employee.
package abc;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Servlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session=request.getSession(false);
String n=(String)session.getAttribute("name");
String a=(String)session.getAttribute("age");
String s=(String)session.getAttribute("sal");
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Servlet2</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Employee Information " + "</h1>");
out.println("<h3>Employee Name " + n + "</h3>");
out.println("<h3>Employee age " + a + "</h3>");
out.println("<h3>Employee Salary " + s + "</h3>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
}
iv)Screen Shots
1.User has to Enter the information.
2. Successful registration
3.Display Employee Inforamtion
No comments:
Post a Comment