0% found this document useful (0 votes)
82 views15 pages

Servlets Demo Programs

This document discusses several examples of servlet programming in Java: 1. Form processing using the getParameter() method to retrieve form data submitted via GET. 2. Reading all form parameters submitted via POST, including handling single and multiple values. 3. Session tracking to maintain user session data across requests using the HttpSession object. 4. Page redirection after form submission using the sendRedirect() method to redirect the client to a success page.

Uploaded by

AKHILA KHODAY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views15 pages

Servlets Demo Programs

This document discusses several examples of servlet programming in Java: 1. Form processing using the getParameter() method to retrieve form data submitted via GET. 2. Reading all form parameters submitted via POST, including handling single and multiple values. 3. Session tracking to maintain user session data across requests using the HttpSession object. 4. Page redirection after form submission using the sendRedirect() method to redirect the client to a success page.

Uploaded by

AKHILA KHODAY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

// Form Processing Using GetParamter

// Import required java libraries


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class


public class HelloForm extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Using GET Method to Read Form Data";
out.println("<html>\n" + "<head><title>" + title
+ "</title></head>\n"
+ "<body bgcolor=\"#f0f0f0\">\n"
+ "<h1 align=\"center\">" + title
+"</h1>\n"
+ "<ul>\n"
+ " <li><b>First Name</b>:”
+ request.getParameter("first_name")
+ "</li>\n"
+ " <li><b>Last Name</b>: "
+ request.getParameter("last_name")
+ "</li>\n"
+ "</ul>\n"
+ "</body></html>");
}
}
// Form Processing Using HTML Page and GetParamter() method
FileName: Hello.html

<html>
<body>
<form action="HelloForm" method="GET">
First Name: <input type="text" name="first_name">
<br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
Run the Hello.html page using server
Output as follows:
Enter First Name as KLE and Last Name as BCA then Click on
submit button
Output as follow
//Reading All Form Parameters

CheckBoxDemo.html
<html>
<body>
<form action="ReadParams" method="POST" target="_blank">
<input type="checkbox" name="maths" checked="checked" /> Maths
<input type="checkbox" name="physics" /> Physics
<input type="checkbox" name="chemistry" checked="checked" /> Chem
<input type="submit" value="Select Subject" />
</form>
</body>
</html>

ReadAllParam.java

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

// Extend HttpServlet class


public class ReadAllParam extends HttpServlet {
// Method to handle GET method request.
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading All Form Parameters";
out.println("<html>\n" + "<head><title>" + title +
"</title></head>\n"
+ "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" +
title + "</h1>\n"
+ "<table width=\"100%\" border=\"1\" align=\"center\">\n" +
"<tr bgcolor=\"#949494\">\n"
+ "<th>Param Name</th><th>Param Value(s)</th>\n" + "</tr>\n");

Enumeration paramNames = request.getParameterNames();

while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n<td>");

String[] paramValues = request.getParameterValues(paramName);


// Read single valued data
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.println("<i>No Value</i>");
else
out.println(paramValue);
}
else {
// Read multiple valued data
out.println("<ul>");
for (int i = 0; i < paramValues.length; i++) {
out.println("<li>" + paramValues[i]+ "</li>");
}
out.println("</ul>");
}
}
out.println("</tr>\n</table>\n</body></html>");
}

// Method to handle POST method request.


public void doPost(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {

doGet(request, response);
}
}
Run checkboxDemo.html Output is as follows

After Submit Output is as follows


Session Tracking Example

Index.html
<html>
<head>
<title>Handling Session</title>
</head>
<body>
<form action="/ServletDemo/SessionServlet" method="GET">
<label>Username</label>
<input type="text" name="name" />
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Note: SessionServlet.java and SessionTrack.java are created in ServletDemo


package

SessionServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SessionServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n = request.getParameter("name");
out.print("Welcome:" + n);
HttpSession session = request.getSession();
session.setAttribute("uname", n);
out.print("<a href='SessionTrack'> Check Session Tracking</a>");
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
SessionTrack.java

import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionTrack extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
String n = (String) session.getAttribute("uname");
out.print("Hello " + n);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

Web.xml
Add the following Code
<servlet>
<servlet-name>SessionServlet</servlet-name>
<servlet-class>SessionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionServlet</servlet-name>
<url-pattern>/SessionServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SessionTrack</servlet-name>
<servlet-class>SessionTrack</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionTrack</servlet-name>
<url-pattern>/SessionTrack</url-pattern>
</servlet-mapping>
Output
1.Run Index.html

2.Redirect to sessionservlet.java
3.Click on check Session Tracking link it Redirects to
sessionTrack.java
Page Redirection Example

CheckBoxDemo.html
<html>
<body>
<form action="ReadParams" method="POST">
<input type="checkbox" name="maths" checked="checked" /> Maths
<input type="checkbox" name="physics" /> Physics
<input type="checkbox" name="chemistry" checked="checked" /> Chem
<input type="submit" value="Select Subject" />
</form>
</body>
</html>

ReadAllParam.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

// Extend HttpServlet class


public class ReadAllParam extends HttpServlet {
// Method to handle GET method request.
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading All Form Parameters";
out.println("<html>\n" + "<head><title>" + title +
"</title></head>\n"
+ "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" +
title + "</h1>\n"
+ "<table width=\"100%\" border=\"1\" align=\"center\">\n" +
"<tr bgcolor=\"#949494\">\n"
+ "<th>Param Name</th><th>Param Value(s)</th>\n" + "</tr>\n");

Enumeration paramNames = request.getParameterNames();

while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n<td>");

String[] paramValues = request.getParameterValues(paramName);


// Read single valued data
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.println("<i>No Value</i>");
else
out.println(paramValue);
}
else {
// Read multiple valued data
out.println("<ul>");
for (int i = 0; i < paramValues.length; i++) {
out.println("<li>" + paramValues[i]+ "</li>");
}
out.println("</ul>");
}
}
out.println("</tr>\n</table>\n</body></html>");
}

// Method to handle POST method request.


public void doPost(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {

doGet(request, response);

response.sendRedirect("/ServletDemo/success.html");
}
}
Success.html
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Success</h1>
</body>
</html>

Output

You might also like