Friday, 1 August 2014

getPathInfo()

public String getPathInfo()
Returns any extra path information associated with the URL the client sent when it made this request. The extra path information follows the servlet path but precedes the query string and will start with a "/" character. This method returns null if there was no extra path information.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Get Books</title>
    </head>
    <body>
        <form method="get" action="/servlet/GetBooks/GetMyBook">
           <select name="author">
                <option>Yashavant Kanetkar</option>
                <option>Deitel</option>
                <option>Herbert Schildt</option>
                <option>Bruce Eckel</option>
          </select> 
            <input type="submit" value="submit" />
        </form>
    </body>
</html>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>GetBooks</servlet-name>
        <servlet-class>GetBooks</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>GetBooks</servlet-name>
        <url-pattern>/GetBooks/GetMyBook</url-pattern>
    </servlet-mapping>
</web-app>

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class GetBooks extends HttpServlet {
    
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       PrintWriter out = res.getWriter();
       String author;
       List<String> books;
       Iterator<String> iter;
       StringBuilder body = new StringBuilder();
       res.setContentType("text/html");
      
       String header = "<html>" +
                    "<head>" +
                    "<title> Hello World </title>" +
                    "</head>" +
                    "<body>" +
                    "<h1> <br />";                     
       String footer = "</h1> </body>" +
                    "</html>";
       
       String contextPath = req.getContextPath();
       String servletPath = req.getServletPath();
       String pathInfo = req.getPathInfo();
       
       body.append("ContextPath ").append(contextPath).append("<br />");
       body.append("Servlet Path ").append(servletPath).append("<br />");
       body.append("Path Info ").append(pathInfo).append("<br />");
       out.println(header + "" + body + footer);
    }
}

Output





Prevoius                                                 Next                                                 Home

getServletPath()

public String getServletPath()
Returns the part of this request's URL that calls the servlet. This method will return an empty string ("") if the servlet used to process this request was matched using the "/*" pattern.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Get Books</title>
    </head>
    <body>
        <form method="get" action="/servlet/GetBooks/GetMyBook">
           <select name="author">
                <option>Yashavant Kanetkar</option>
                <option>Deitel</option>
                <option>Herbert Schildt</option>
                <option>Bruce Eckel</option>
          </select> 
            <input type="submit" value="submit" />
        </form>
    </body>
</html>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>GetBooks</servlet-name>
        <servlet-class>GetBooks</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>GetBooks</servlet-name>
        <url-pattern>/GetBooks/GetMyBook</url-pattern>
    </servlet-mapping>
</web-app>

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class GetBooks extends HttpServlet {
    
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       PrintWriter out = res.getWriter();
       String author;
       List<String> books;
       Iterator<String> iter;
       StringBuilder body = new StringBuilder();
       res.setContentType("text/html");
      
       String header = "<html>" +
                    "<head>" +
                    "<title> Hello World </title>" +
                    "</head>" +
                    "<body>" +
                    "<h1> <br />";                     
       String footer = "</h1> </body>" +
                    "</html>";
       
       String contextPath = req.getContextPath();
       String servletPath = req.getServletPath();
       String pathInfo = req.getPathInfo();
       
       body.append("ContextPath ").append(contextPath).append("<br />");
       body.append("Servlet Path ").append(servletPath).append("<br />");
       body.append("Path Info ").append(pathInfo).append("<br />");
       out.println(header + "" + body + footer);
    }
}

Output






Prevoius                                                 Next                                                 Home

getContextPath()

public String getContextPath()
Returns the portion of the request URI that indicates the context of the request. For servlets in the default (root) context, this method returns "".

Note:
It is possible that a servlet container may match a context by more than one context path. In such cases this method will return the actual context path used by the request and it may differ from the path returned by the ServletContext.getContextPath() method. The context path returned by ServletContext.getContextPath() should be considered as the prime or preferred context path of the application.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Get Books</title>
    </head>
    <body>
        <form method="get" action="/servlet/GetBooks/GetMyBook">
           <select name="author">
                <option>Yashavant Kanetkar</option>
                <option>Deitel</option>
                <option>Herbert Schildt</option>
                <option>Bruce Eckel</option>
          </select> 
            <input type="submit" value="submit" />
        </form>
    </body>
</html>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>GetBooks</servlet-name>
        <servlet-class>GetBooks</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>GetBooks</servlet-name>
        <url-pattern>/GetBooks/GetMyBook</url-pattern>
    </servlet-mapping>
</web-app>

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class GetBooks extends HttpServlet {
    
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       PrintWriter out = res.getWriter();
       String author;
       List<String> books;
       Iterator<String> iter;
       StringBuilder body = new StringBuilder();
       res.setContentType("text/html");
      
       String header = "<html>" +
                    "<head>" +
                    "<title> Hello World </title>" +
                    "</head>" +
                    "<body>" +
                    "<h1> <br />";                     
       String footer = "</h1> </body>" +
                    "</html>";
       
       String contextPath = req.getContextPath();
       String servletPath = req.getServletPath();
       String pathInfo = req.getPathInfo();
       
       body.append("ContextPath ").append(contextPath).append("<br />");
       body.append("Servlet Path ").append(servletPath).append("<br />");
       body.append("Path Info ").append(pathInfo).append("<br />");
       out.println(header + "" + body + footer);
    }
}

Output





Prevoius                                                 Next                                                 Home

Thursday, 31 July 2014

Request Path Elements

Request path leads to a servlet, that process the request.

RequestPath = ContextPath + ServletPath + PathInfo

Servlet Path
This path directly corresponds to the mapping which activated this request.

Context Path
The path prefix associated with the ServletContext that this servlet is a part of.

PathInfo
The part of the request path that is not part of the Context Path or the
Servlet Path. It is either null if there is no extra path, or is a string with a leading
'/'.

HttpServletRequest interface provides below methods, to get servlet path, context path and path information.
     getContextPath()
     getServletPath()
     getPathInfo()




Prevoius                                                 Next                                                 Home

long getDateHeader(String name)

Returns the value of the specified request header as a long value that represents a Date object. Use this method with headers that contain dates, such as If-Modified-Since. If the header can't be converted to a date, the method throws an IllegalArgumentException.If the request did not have a header of the specified name, this method returns -1


Prevoius                                                 Next                                                 Home

int getIntHeader(String name)

Returns the value of the specified request header as an int. If the header can't be converted as a number, then NumberFormatException thrown. If the request does not have a header of the specified name, this method returns -1




Prevoius                                                 Next                                                 Home

getHeader (String name)

public String getHeader(String name)
Returns the value of the specified request header as a String. If there are multiple headers with the same name, this method returns the first head in the request.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="get" action="/servlet/HelloWorld">
            <input type="submit">
        </form>
    </body>
</html>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>
</web-app>

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class HelloWorld extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       try(PrintWriter out = res.getWriter()) {
            res.setContentType("text/html");
            StringBuilder s1 = new StringBuilder();
            
            Enumeration<String> headers = req.getHeaderNames();
            
            while(headers.hasMoreElements()){
                String headerName = headers.nextElement();
                s1.append(headerName).append(":");
                
                /* Get the Header Values */
                s1.append(req.getHeader(headerName)).append("<br />");                
            }
            
            String str = "<html>" +
                         "<head>" +
                         "<title> Hello World </title>" +
                         "</head>" +
                         "<body>" +
                         "<h1> Headers are <br />" + s1 +
                         "</h1> </body>" +
                         "</html>";
            out.println(str);
       }
   } 
  
}

Output




 
Prevoius                                                 Next                                                 Home