Friday, 1 August 2014

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

No comments:

Post a Comment