Reading Servlet Parameters

Estudies4you
Reading Servlet Parameters
Classes
GenericServlet Class: Provides the logic/implementation for life cycle methods of Servlet. This class implements interfaces like Servlet and ServletConfig
ServletlnputStream Class: Extends InputStream [with its readLine() method] that provides user to read data from client request
ServletOutputStream Class: Extends OutputStream [with its print() method] that provides user to write data to a client response
ServletException Class: Thrown when the problem occur with servlet A UnavailableException Class: Thrown when the servlet is not available. This class extends ServletException

Reading Servlet Parameters
Example 3
  • This example explains how to get the list of parameters that are passed to servlet in its Request from client. (We will use the classes/interfaces that we learned in earlier slides)
  • Create following files in a folder (Please check next slide for code)
    • Java FileReadParamsServlet.java Contains logic to get the params details
    • DD web.xml Contains deployment details. Deployment Descriptor
    • HTMLFile index.html Contains html that allows user to enter details
  • Compile the Java File as you did in earlier example
  • Create below structure with files that you created above
    • Folder "ReadParamsServlet" contains
    • I__"WEB-INF" Folder and index.html
    • I__"WEB-INF" Folder contains "classes" Folder and web.xml File.
    • I__"classes" Folder contains "ReadParamsServlet.class" File
  • Deploy the ReadParamsServlet folder inside webapps folder of Tomcat
  • Bounce the Server
  • Send request using "http://localhost:8550/ReadParamsServlet/index.html"

ReadParamsServlet.java: This prepares the response as HTML file that displays request parameters
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class ReadParamsServlet extends GenericServlet {
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
Enumeration list = request.getParameterNames();
while(list.hasMoreElements()) {
String paramname = (String)list.nextElement();
pw.print(paramname + " = ");
String paramvalue = request.getParameter(paramname); pw.println(paramvalue);
}
pw.close();
}
}
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<html>");
out.println("<head>");
out.println("<title>InitParams! </title>");
outprintln("</head>");
out.println("<body>");
out.println("<h1>Init Params from Serylet!</h1>");
Iterator<Map.Entry> iter = initParamsCollection.entrySet().iterator();
while (iter.hasNextQ) Map.Entry entry = iter.next();
String key = (String)entry.getKey();
String value = (String)entry.getValue();
out.println("InitParameter :" +key +" Its Value:"+value+ …..")
}
out.println("</body>");
out.println("</html>");
out.close();
}
}
  • web.xml
    • <servlet> tag maps your servlet class with internal name
    • <servlet-mapping> tag maps internal name with url-pattern that your invoke
-<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmins:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
-<servlet>
<servlet-name>paramsservlet</servlet-name>
<servlet-class>ReadParamsServiet</servlet-class>
</servlet> <servlet-mapping>
-<servlet-name>paramsservlet</servlet-name>
<url-pattern>/RegisterStudent.do</url-pattern>
</servlet-mapping>
</web-app>
  • index.html
<html>
<body>
<center>
<hl align="center">Welcome to Student Registration Page...</h1>
<form name="StudentForm" method="post" action="RegisterStudent.do">
<table>
<tr>
<td><B>Name of Student : </td>
<td><input type=textbox name="StudentNameParam" size="25" value=""></td>
</tr>
<tr>
<td><B>Registration # : </td>
<td><input type=textbox name="RollNumberParam" s' value=""> </td>
</tr>
<tr>
<td><B>Father's Name : </td>
<td><input type=textbox name="FatherName" size="25" value="">'</td>
</tr>
</table>
</form>
</center>
</body>
</html>
Reading Servlet Parameters

To Top