java - How does servlet container process this http request? -
i have created dynamic web project
using eclipse first time work servlet , jsp.
below servlet code,
package com.example.tutorial; 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; public class servletexample extends httpservlet { private static final long serialversionuid = 1l; protected void service(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { printwriter out = response.getwriter(); string firstname = request.getparameter("firstname"); string lastname = request.getparameter("lastname"); out.println(firstname + " " + lastname); } }
and corresponding web.xml,
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="webapp_id" version="2.5"> <display-name>servletsjspexample</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>servletexample</display-name> <servlet-name>servletexample</servlet-name> <servlet-class>com.example.tutorial.servletexample</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletexample</servlet-name> <url-pattern>/servletexample</url-pattern> </servlet-mapping> </web-app>
i have written index.jsp
has below form:
<?xml version="1.0" encoding="iso-8859-1" ?> <%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>insert title here</title> </head> <body> <form action="servletexample" method="post" > <table border="0"> <tr> <td>first name:</td> <td><input type="text" name="firstname" /></td> </tr> <tr> <td>last name:</td> <td><input type="text" name="lastname" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="submit" /></td> </tr> </table> </form> </body> </html>
in first case, within eclipse, if select run -> run on server
, browser within eclipse shows presentation code of index.jsp
uri: http://localhost:8081/servletsjspexample/
i later add below line of code in servlet,
this.getservletcontext().getrequestdispatcher("/index.jsp").forward(request, response);
as shown below,
public class servletexample extends httpservlet { private static final long serialversionuid = 1l; protected void service(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { printwriter out = response.getwriter(); this.getservletcontext().getrequestdispatcher("/index.jsp").forward(request, response); string firstname = request.getparameter("firstname"); string lastname = request.getparameter("lastname"); out.println(firstname + " " + lastname); } }
in second case, within eclipse, if select run -> run on server
, browser within eclipse shows presentation code of index.jsp
uri: http://localhost:8081/servletsjspexample/servletexample
so,
wrt these 2 cases, how uri getting changed? how control flowing servlet container application servletsjspexample
before , after adding line of code this.getservletcontext().getrequestdispatcher("/index.jsp").forward(request, response);
please me understand this!!!
note: servletsjspexample
"dynamic web project" name in eclipse
first, forget eclipse , how launch program within it. servlets, url-mappings, , urls hit.
in first sample, hit
http://localhost:8081/servletsjspexample/
assuming servletsjspexample
context root, you're reaching root of app. since have welcome-file
registered (which jsp)
<welcome-file>index.jsp</welcome-file>
the servlet container serve directly.
in second case, send request to
http://localhost:8081/servletsjspexample/servletexample
which has path of /servletexample
. have servletexample
servlet mapped to
<url-pattern>/servletexample</url-pattern>
so servlet container chooses servletexample
servlet handle request. handling request means invoking service
method, goes on invoke
this.getservletcontext().getrequestdispatcher("/index.jsp").forward(request, response);
with call getrequestdispatcher
container
returns
requestdispatcher
object acts wrapper resource located @ given path.
the resource located @ given path, in case, jsp (another servlet
). call forward
, are, down line, invoking service
on jsp servlet. renders content of jsp , sends response body.
Comments
Post a Comment