Servlet Examples
Servlet Examples
1.Hello World
output:
code:
1.import java.io.*;
2.import javax.servlet.*;
3.import javax.servlet.http.*;
4.
5.public class HelloWorld extends HttpServlet {
6.
7. public void doGet(HttpServletRequest request, HttpServletResponse response)
8. throws IOException, ServletException
9. {
10. response.setContentType("text/html");
11. PrintWriter out = response.getWriter();
12. out.println("<html>");
13. out.println("<head>");
14. out.println("<title>Hello World!</title>");
15. out.println("</head>");
16. out.println("<body>");
17. out.println("<h1>Hello World!</h1>");
18. out.println("</body>");
19. out.println("</html>");
20. }
21.}
2.Request Info
output:
code:
1.import java.io.*;
2.import javax.servlet.*;
3.import javax.servlet.http.*;
4.
5.public class RequestInfo extends HttpServlet {
6.
7. public void doGet(HttpServletRequest request, HttpServletResponse response)
8. throws IOException, ServletException
9. {
10. response.setContentType("text/html");
11. PrintWriter out = response.getWriter();
12. out.println("<html>");
13. out.println("<body>");
14. out.println("<head>");
15. out.println("<title>Request Information Example</title>");
16. out.println("</head>");
17. out.println("<body>");
18. out.println("<h3>Request Information Example</h3>");
19. out.println("Method: " + request.getMethod());
20. out.println("Request URI: " + request.getRequestURI());
21. out.println("Protocol: " + request.getProtocol());
22. out.println("PathInfo: " + request.getPathInfo());
23. out.println("Remote Address: " + request.getRemoteAddr());
24. out.println("</body>");
25. out.println("</html>");
26. }
27.
28. /**
29. * We are going to perform the same operations for POST requests
30. * as for GET methods, so this method just sends the request to
31. * the doGet method.
32. */
33.
34. public void doPost(HttpServletRequest request, HttpServletResponse response)
35. throws IOException, ServletException
36. {
37. doGet(request, response);
38. }
39.}
3.Request Headers
output:
code:
1.import java.io.*;
2.import java.util.*;
3.import javax.servlet.*;
4.import javax.servlet.http.*;
5.
6.public class RequestHeaderExample extends HttpServlet {
7.
8. public void doGet(HttpServletRequest request, HttpServletResponse response)
9. throws IOException, ServletException
10. {
11. response.setContentType("text/html");
12. PrintWriter out = response.getWriter();
13. Enumeration e = request.getHeaderNames();
14. while (e.hasMoreElements()) {
15. String name = (String)e.nextElement();
16. String value = request.getHeader(name);
17. out.println(name + " = " + value);
18. }
19. }
20.}
4. Request Parameters
output:
code:
1.import java.io.*;
2.import java.util.*;
3.import javax.servlet.*;
4.import javax.servlet.http.*;
5.
6.public class RequestParamExample extends HttpServlet {
7.
8. public void doGet(HttpServletRequest request, HttpServletResponse response)
9. throws IOException, ServletException
10. {
11. response.setContentType("text/html");
12. PrintWriter out = response.getWriter();
13. out.println("<html>");
14. out.println("<head>");
15. out.println("<title>Request Parameters Example</title>");
16. out.println("</head>");
17. out.println("<body>");
18. out.println("<h3>Request Parameters Example</h3>");
19. out.println("Parameters in this request:<br>");
20. if (firstName != null || lastName != null) {
21. out.println("First Name:");
22. out.println(" = " + HTMLFilter.filter(firstName) + "<br>");
23. out.println("Last Name:");
24. out.println(" = " + HTMLFilter.filter(lastName));
25. } else {
26. out.println("No Parameters, Please enter some");
27. }
28. out.println("<P>");
29. out.print("<form action=\"");
30. out.print("RequestParamExample\" ");
31. out.println("method=POST>");
32. out.println("First Name:");
33. out.println("<input type=text size=20 name=firstname>");
34. out.println("<br>");
35. out.println("Last Name:");
36. out.println("<input type=text size=20 name=lastname>");
37. out.println("<br>");
38. out.println("<input type=submit>");
39. out.println("</form>");
40. out.println("</body>");
41. out.println("</html>");
42. }
43.
44. public void doPost(HttpServletRequest request, HttpServletResponse res)
45. throws IOException, ServletException
46. {
47. doGet(request, response);
48. }
49.}
5. Cookies
output:
code:
1.import java.io.*;
2.import javax.servlet.*;
3.import javax.servlet.http.*;
4.
5.public class CookieExample extends HttpServlet {
6.
7. public void doGet(HttpServletRequest request, HttpServletResponse response)
8. throws IOException, ServletException
9. {
10. response.setContentType("text/html");
11. PrintWriter out = response.getWriter();
12.
13. // print out cookies
14.
15. Cookie[] cookies = request.getCookies();
16. for (int i = 0; i < cookies.length; i++) {
17. Cookie c = cookies[i];
18. String name = c.getName();
19. String value = c.getValue();
20. out.println(name + " = " + value);
21. }
22.
23. // set a cookie
24.
25. String name = request.getParameter("cookieName");
26. if (name != null && name.length() > 0) {
27. String value = request.getParameter("cookieValue");
28. Cookie c = new Cookie(name, value);
29. response.addCookie(c);
30. }
31. }
32.}
6. Sessions
output:
code:
1.import java.io.*;
2.import java.util.*;
3.import javax.servlet.*;
4.import javax.servlet.http.*;
5.
6.public class SessionExample extends HttpServlet {
7.
8. public void doGet(HttpServletRequest request, HttpServletResponse response)
9. throws IOException, ServletException
10. {
11. response.setContentType("text/html");
12. PrintWriter out = response.getWriter();
13.
14. HttpSession session = request.getSession(true);
15.
16. // print session info
17.
18. Date created = new Date(session.getCreationTime());
19. Date accessed = new Date(session.getLastAccessedTime());
20. out.println("ID " + session.getId());
21. out.println("Created: " + created);
22. out.println("Last Accessed: " + accessed);
23.
24. // set session info if needed
25.
26. String dataName = request.getParameter("dataName");
27. if (dataName != null && dataName.length() > 0) {
28. String dataValue = request.getParameter("dataValue");
29. session.setAttribute(dataName, dataValue);
30. }
31.
32. // print session contents
33.
34. Enumeration e = session.getAttributeNames();
35. while (e.hasMoreElements()) {
36. String name = (String)e.nextElement();
37. String value = session.getAttribute(name).toString();
38. out.println(name + " = " + value);
39. }
40. }
41.}