GTU WTAD Practical 12 : Write a Servlet to display parameters available on request. .

GTU WTAD Practical 12 : Write a Servlet to display parameters available on request. .

GTU WTAD Practical 12

Another important point that is very command and very useful to get value from the request url. When you send query string data using servlet then you can receive this data using getParameter() method. Suppose you have database and you would like to edit record then best way to send record id as query string to the page, this value can be get using getParameter() method of request object.

Jump to GTU WTAD Practical 13

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu12\

<%
	response.sendRedirect("./AvailableParameters?id=1&first=Adarsh&last=Patel");
%>

 

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu12\WEB-INF\

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">  
  
  <display-name>GTU WTAD Practical 12 </display-name>
  <description>Write a Servlet to display parameters available on request.</description>
  
  <servlet>
    <servlet-name>AvailableParameters</servlet-name>
    <servlet-class>AvailableParameters</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>AvailableParameters</servlet-name>
      <url-pattern>/AvailableParameters</url-pattern>
  </servlet-mapping> 
  
</web-app>

 

File Name : AvailableParameters.java

File Location : Tomcat_Apache\webapps\gtu12\WEB-INF\classes

/*
		GTU WTAD Practical 12 : Write a Servlet to display parameters available on request. . 
*/

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

public class AvailableParameters extends HttpServlet
{
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html><head><title>GTU WTAD Practical 12  : Write a Servlet to display parameters available on request. .</title></head><body><center><h1>GTU WTAD Practical 12 : Write a Servlet to display parameters available on request.   </h1></center><br><br>");
		
		//Display Header Name and Value in Table Format 
		out.println("<table border=1>");
		out.println("<tr><th>Parameter Name</th><th>Parameter Values</th></tr>");
		Enumeration e = request.getParameterNames();	
		while(e.hasMoreElements())
		{
			String header_name = (String) e.nextElement();
			String header_value = (String) request.getParameter(header_name);
			out.println("<tr><td><b>"+header_name + "</b></td><td> " + header_value + "</td></tr>");
		}
		out.println("</table>");
		
		out.println("</body></html>");
	}
}

 

Output

GTU WTAD Practical 12

Leave a Reply