More Related Content
Similar to Java Servlets (20)
More from BG Java EE Course (20)
Java Servlets
- 28. Hello Servlet – Example public void doGet(HttpServletRequest r equest, HttpServletResponse r esponse) throws ServletException, IOException { r esponse.setContentType("text/html"); ServletOutputStream out = r esponse.getOutputStream(); String userName = r equest.getParameter("user_name"); out.println("<html> <head> "); out.println("<title>Hello Servlet</title>"); out.println("</head><body>"); out.println("<h1>Hello, " + userName + "</h1>"); out.println("</body></html>"); } Hello Servlet . java
- 35. Image Counter Servlet (2) import javax.servlet.*; import javax.servlet.http.*; ... public class ImageCounterServlet extends HttpServlet { private String mStartDate; private int mVisitCounter; public void init() { mStartDate = (new Date()).toString(); mVisitCounter = 0; } public BufferedImage createImage(String msg) { ... }
- 36. Image Counter Servlet (3) public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String msg; synchronized(this) { mVisitCounter++; msg = "" + mVisitCounter + " visits since " + mStartDate; } BufferedImage image = createImage(msg); response.setContentType("image/jpeg"); OutputStream out = response.getOutputStream(); // Encode the image in JPEG format and // write the image to the output stream } }
- 50. Login Form <html> <head><title>Login</title></head> <body> <form method="POST" action="LoginServlet"> Please login:<br> Username: <input type="text" name="username"><br> Password: <input type="password" name="password"><br> <input type="submit" value="Login"> </form> </body> </html> LoginForm.html
- 51. Login Servlet public class LoginServlet extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { String username = req.getParameter("username"); String password = req.getParameter("password"); PrintWriter out = resp.getWriter(); if (isLoginValid(username, password)) { HttpSession session = req.getSession(); session.setAttribute("USER", username); response.sendRedirect("MainServlet"); } else { response.sendRedirect("InvalidLogin.html"); } } } Login Servlet . java
- 52. Main Servlet public class MainServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = request.getSession(); String userName = (String) s ession.getAttribute("USER"); if (userName != null) { response.setContentType("text/html"); ServletOutputStream out = resp.getOutputStream(); out.println("<html> <body><h1> "); out.println("Hello, " + userName + "! "); out.println(" </h1> </body></html>"); } else { response.sendRedirect("LoginForm.html"); } } } MainServlet.java
- 53. Logout Servlet public class LogoutServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.invalidate(); response.setContentType("text/html"); ServletOutputStream out = response.getOutputStream(); out.println("<html><head>"); out.println("<title>Logout</title></head>"); out.println("<body>"); out.println("<h1>Logout successfull.</h1>"); out.println("</body></html>"); } } LogoutServlet.java
- 54. Invalid Login Page <html> <head> <title>Error</title> </head> <body> <h1>Invalid login!</h1> Please <a href="LoginForm.html">try again</a>. </body> </html> InvalidLogin.html
Editor's Notes
- ## * * 07/16/96
- ## * * 07/16/96
- ## * * 07/16/96 Example of HTTP GET: Google search Example of HTTP POST: Login page
- ## * * 07/16/96
- ## * * 07/16/96
- ## * * 07/16/96
- ## * * 07/16/96
- Note: As of Servlet 2.2, the getValue() method is now deprecated. Use getAttribute() instead.
- Note: As of Servlet 2.2, the getValue() method is now deprecated. Use getAttribute() instead.