addheader

Sunday, 7 October 2012

weekend class meterials(servlets)


TUTIORALS FROM MADHAV:


     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY  


public abstract class GenericServlet
extends java.lang.Object
implements Servlet, ServletConfig, java.io.Serializable
Defines a generic, protocol-independent servlet. To write an HTTP servlet for use on the Web, extend HttpServlet instead.
Methods:
1.     Init
2.     Service
3.     Destroy
4.     getServletConfig
5.     getServletContext

6.     getInitParameter

7.     getInitParameterNames


init
public void init() throws ServletException

A convenience method which can be overridden so that there's no need to call super.init(config).
Instead of overriding init(ServletConfig), simply override this method and it will be called by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via getServletConfig().
Throws:
ServletException - if an exception occurs that interrupts the servlet's normal operation

Service:
public abstract void service(ServletRequest req,
                             ServletResponse res)
                      throws ServletException,
                             java.io.IOException
Called by the servlet container to allow the servlet to respond to a request.
This method is declared abstract so subclasses, such as HttpServlet, must override it.
Specified by:
service in interface Servlet
Parameters:
req - the ServletRequest object that contains the client's request
res - the ServletResponse object that will contain the servlet's response
Throws:
ServletException - if an exception occurs that interferes with the servlet's normal operation occurred
java.io.IOException - if an input or output exception occurs
destroy:
public void destroy()
Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. See Servlet.destroy().
Specified by:
destroy in interface Servlet
getServletConfig:
public ServletConfig getServletConfig()
Returns this servlet's ServletConfig object.
Specified by:
Returns:
ServletConfig the ServletConfig object that initialized this servlet
getServletContext:
public ServletContext getServletContext()
Returns a reference to the ServletContext in which this servlet is running. See ServletConfig.getServletContext().
This method is supplied for convenience. It gets the context from the servlet's ServletConfig object.
Specified by:
Returns:
ServletContext the ServletContext object passed to this servlet by the init method

public interface ServletRequest
Defines an object to provide client request information to a servlet. The servlet container creates a ServletRequest object and passes it as an argument to the servlet's service method.


Method:
1.      getAttribute
2.     getParameter
3.     getRequestDispatcher

java.lang.Object getAttribute(java.lang.String name)
          Returns the value of the named attribute as an Object, or null if no attribute of the given name exists
getParameter
public java.lang.String getParameter(java.lang.String name)
Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.
getRequestDispatcher
public RequestDispatcher getRequestDispatcher(java.lang.String path)
Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.

public interface ServletResponse
Defines an object to assist a servlet in sending a response to the client. The servlet container creates a ServletResponse object and passes it as an argument to the servlet's service method.
Methods:
1.      setContentType
2.      setContentLength
3.     getWriter

 void setContentType(java.lang.String type)
          Sets the content type of the response being sent to the client, if the response has not been committed yet.

 void setContentLength(int len)
          Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length header.
getWriter
public java.io.PrintWriter getWriter()
                              throws java.io.IOException
Returns a PrintWriter object that can send character text to the client.
Example program:
//this will describes simple web application;



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
          
 
   <servlet>
  <servlet-name>gmr</servlet-name>
  <servlet-class>softech.MyServlet</servlet-class>
  </servlet>
 
  <servlet-mapping>
  <servlet-name>gmr</servlet-name>
  <url-pattern>/first</url-pattern>
  </servlet-mapping>
 
 
</web-app>


package softech;

import java.io.PrintWriter;

import javax.servlet.*;


public class MyServlet extends GenericServlet {
      public void service(ServletRequest req,ServletResponse res)
      {
            try
            {
            res.setContentType("text");
            PrintWriter out=res.getWriter();
            out.println("hai this is my firstservlet");
     
            }
            catch(Exception e)
            {    
            }          
      }
}


public interface ServletConfig
A servlet configuration object used by a servlet container to pass information to a servlet during initialization. This object is specific to a servlet.
Methods:
1.     getServletContext
2.     setAttribute
3.     getAttribute
public ServletContext getServletContext()
Returns a reference to the ServletContext in which the caller is executing.
Returns:
a ServletContext object, used by the caller to interact with its servlet container

`                 setAttribute
public void setAttribute(java.lang.String name,
                         java.lang.Object object)
Binds an object to a given attribute name in this servlet context. If the name specified is already used for an attribute, this method will replace the attribute with the new to the new attribute.
           getAttribute
public java.lang.Object getAttribute(java.lang.String name)
Returns the servlet container attribute with the given name, or null if there is no attribute by that name.
               
Example progrm:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
//this program will describes working with ServletConfig object


form.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>form.html</title>
     
  </head>
 
  <body>
    This is my HTML page. <br>
   
    <form action="first">
    name:<input type="text" name="name"><br>
    <input type="submit" value="send">
    </form>
  </body>
</html>

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app >
  
  <servlet>
  <servlet-name>gmr</servlet-name>
  <servlet-class>softech.ConfigExample</servlet-class>
  <init-param>
        <param-name>loc</param-name>
        <param-value>hyderabad</param-value>
    </init-param>
 
  </servlet>
 
  <servlet-mapping>
  <servlet-name>gmr</servlet-name>
  <url-pattern>/first</url-pattern>
  </servlet-mapping>
  
 
 
</web-app>

ConfigExample.java:

package softech;

import java.io.PrintWriter;

import javax.servlet.*;

public class ConfigExample extends GenericServlet {
     
      public void service(ServletRequest req,ServletResponse res)
      {
            try
            {
            String name=req.getParameter("name");    
            res.setContentType("text");
            PrintWriter out=res.getWriter();
            out.println("hai madhav how r u");
            out.println("<br>");
            out.println("name="+name);
            ServletConfig cf=getServletConfig();
            String loc=cf.getInitParameter("loc");
            out.println("loc="+loc);
           
            }
            catch(Exception e)
            {
                       
            }
      }
}



public interface ServletContext
Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.
There is one context per "web application" per Java Virtual Machine.
Methods:
1.     setAttribute
2.     getAttribute
setAttribute
public void setAttribute(java.lang.String name,
                         java.lang.Object object)
Binds an object to a given attribute name in this servlet context. If the name specified is already used for an attribute, this method will replace the attribute with the new to the new attribute.
getAttribute
public java.lang.Object getAttribute(java.lang.String name)
Returns the servlet container attribute with the given name, or null if there is no attribute by that name.

Example program:
//this program will describes servletcontext


form.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>form.html</title>
     

  </head>
 
  <body>
    This is my HTML page. <br>
   
    <form action="first">
    name:<input type="text" name="name"><br>
    <input type="submit" value="send">
    </form>
  </body>
</html>

We.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app >
      
  <servlet>
  <servlet-name>gmr</servlet-name>
  <servlet-class>softech.ServletContextExample</servlet-class>
  <init-param>
        <param-name>loc</param-name>
        <param-value>hyderabad</param-value>
    </init-param>
 
  </servlet>
 
  <servlet-mapping>
  <servlet-name>gmr</servlet-name>
  <url-pattern>/first</url-pattern>
  </servlet-mapping>
   <servlet>
  <servlet-name>gmr2</servlet-name>
  <servlet-class>softech.MyServlet</servlet-class>
  <init-param>
        <param-name>loc</param-name>
        <param-value>hyderabad</param-value>
    </init-param>
 
  </servlet>
 
  <servlet-mapping>
  <servlet-name>gmr2</servlet-name>
  <url-pattern>/second</url-pattern>
  </servlet-mapping>
  
 
 
</web-app>



ServletContextExample.java

package softech;

import java.io.PrintWriter;

import javax.servlet.*;

public class ServletContextExample extends GenericServlet {
            public void service(ServletRequest req,ServletResponse res)
            {
                        try
                        {
                        String name=req.getParameter("name");        
                        res.setContentType("text");
                        PrintWriter out=res.getWriter();
                        out.println("hai madhav how r u");
                        out.println("<br>");
                        out.println("name="+name);
                        ServletConfig cf=getServletConfig();
                        String loc=cf.getInitParameter("loc");
                        out.println("loc="+loc);
                        ServletContext sc=cf.getServletContext();
                        sc.setAttribute("title", "softech computer education");
                        String title=(String)sc.getAttribute("title");
                        out.println("<br>");
                        out.println("title="+ title);
                       
                        }
                        catch(Exception e)
                        {
                                   
                                   
                        }
            }
}



MyServlet.java

package softech;

import java.io.PrintWriter;

import javax.servlet.*;

public class MyServlet extends GenericServlet {
            public void service(ServletRequest req,ServletResponse res)
            {
                        try
                        {
                        String name=req.getParameter("name");        
                        res.setContentType("text");
                        PrintWriter out=res.getWriter();
                        out.println("hai madhav how r u");
                        out.println("<br>");
                        out.println("name="+name);
                        ServletConfig cf=getServletConfig();
                        String loc=cf.getInitParameter("loc");
                        out.println("loc="+loc);
                        ServletContext sc=cf.getServletContext();
                        String title=(String)sc.getAttribute("title");
                        out.println("<br>");
                        out.println("title="+ title);
                       
                        }
                        catch(Exception e)
                        {
                                               
                        }
            }
}



public interface RequestDispatcher
Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name
Methods:

1.     Forward

2.     include

Forward

public void forward(ServletRequest request,
                    ServletResponse response)
             throws ServletException,
                    java.io.IOException
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.

include

public void include(ServletRequest request,
                    ServletResponse response)
             throws ServletException,
                    java.io.IOException
Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes.
Example program:
//this program will describes using RequestDispatcher .
url: http://madhav-4485e67f:8181/servletreqdispatcher/form.html
Form.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>form.html</title>
     

  </head>
 
  <body>
    This is my HTML page. <br>
   
    <form action="first">
    name:<input type="text" name="name"><br>
    <input type="submit" value="send">
    </form>
  </body>
</html>


Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
      xmlns="http://java.sun.com/xml/ns/j2ee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
          
  <servlet>
  <servlet-name>gmr</servlet-name>
  <servlet-class>softech.RequestDispatcherEx</servlet-class>
  <init-param>
        <param-name>loc</param-name>
        <param-value>hyderabad</param-value>
    </init-param>
 
  </servlet>
 
  <servlet-mapping>
  <servlet-name>gmr</servlet-name>
  <url-pattern>/first</url-pattern>
  </servlet-mapping>
   <servlet>
  <servlet-name>gmr2</servlet-name>
  <servlet-class>softech.MyServlet</servlet-class>
  <init-param>
        <param-name>loc</param-name>
        <param-value>hyderabad</param-value>
    </init-param>
 
  </servlet>
 
  <servlet-mapping>
  <servlet-name>gmr2</servlet-name>
  <url-pattern>/second</url-pattern>
  </servlet-mapping>
  
 
 
</web-app>



RequestDispatcherEx.java

package softech;

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

public class RequestDispatcherEx extends GenericServlet {

            public void service(ServletRequest req,ServletResponse res)
            {
                        try
                        {
                        String name=req.getParameter("name");        
                        res.setContentType("text/html");
                        PrintWriter out=res.getWriter();
                        out.println("hai madhav how r u");
                        out.println("<br>");
                        out.println("name="+name);
                        ServletConfig cf=getServletConfig();
                        String loc=cf.getInitParameter("loc");
                        out.println("loc="+loc);
                        ServletContext sc=cf.getServletContext();
                        sc.setAttribute("title", "softech computer education");
                        String title=(String)sc.getAttribute("title");
                        out.println("<br>");
                        out.println("title="+ title);
                        RequestDispatcher rd=req.getRequestDispatcher("/second");
                        rd.include(req, res);
                       
                        }
                        catch(Exception e)
                        {
                                    e.printStackTrace();
                                   
                        }
                        }
}




MyServlet.java

package softech;

import java.io.PrintWriter;

import javax.servlet.*;

public class MyServlet extends GenericServlet {
            public void service(ServletRequest req,ServletResponse res)
            {
                        try
                        {
                        String name=req.getParameter("name");        
                        res.setContentType("text");
                        PrintWriter out=res.getWriter();
                        out.println("hai madhav how r u");
                        out.println("<br>");
                        out.println("name="+name);
                        ServletConfig cf=getServletConfig();
                        String loc=cf.getInitParameter("loc");
                        out.println("loc="+loc);
                        ServletContext sc=cf.getServletContext();
                        String title=(String)sc.getAttribute("title");
                        out.println("<br>");
                        out.println("title="+ title);
                       
                        }
                        catch(Exception e)
                        {
                                                                       
                        }         
            }
}




public interface HttpServletRequest
Extends the ServletRequest interface to provide request information for HTTP servlets.
The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods (doGet, doPost, etc).
Methods:
1.     getCookies
2.     getSession
getCookies
public Cookie[] getCookies()
Returns an array containing all of the Cookie objects the client sent with this request. This method returns null if no cookies were sent.
Returns:
an array of all the Cookies included with this request, or null if the request has no cookies
getSession
public HttpSession getSession(boolean create)
Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.
public interface HttpServletResponse
Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response. For example, it has methods to access HTTP headers and cookies.
The servlet container creates an HttpServletResponse object and passes it as an argument to the servlet's service methods (doGet, doPost, etc).

public interface HttpSession
Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.
The servlet container uses this interface to create a session between an HTTP client and an HTTP server. The session persists for a specified time period, across more than one connection or page request from the user. A session usually corresponds to one user, who may visit a site many times. The server can maintain a session in many ways such as using cookies or rewriting URLs.


Methods:
1.     setAttribute
2.     getAttribute
3.     setMaxInactiveInterval

4.     getServletContext

5.     getId
setAttribute
public void setAttribute(java.lang.String name,
                         java.lang.Object value)
Binds an object to this session, using the name specified. If an object of the same name is already bound to the session, the object is replaced.
Parameters:
name - the name to which the object is bound; cannot be null
value - the object to be bound
If the value passed in is null, this has the same effect as calling removeAttribute().
getAttribute
public java.lang.Object getAttribute(java.lang.String name)
Returns the object bound with the specified name in this session, or null if no object is bound under the name.
Parameters:
name - a string specifying the name of the object
Returns:
the object with the specified name
setMaxInactiveInterval
public void setMaxInactiveInterval(int interval)
Specifies the time, in seconds, between client requests before the servlet container will invalidate this session. A negative time indicates the session should never timeout.
Parameters:
interval - An integer specifying the number of seconds

getServletContext

public ServletContext getServletContext()
Returns the ServletContext to which this session belongs.
Returns:
The ServletContext object for the web application
Since:
2.3
getId
public java.lang.String getId()
Returns a string containing the unique identifier assigned to this session. The identifier is assigned by the servlet container and is implementation dependent.
Returns:
a string specifying the identifier assigned to this session

example program:

public interface HttpServletResponse
Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response. For example, it has methods to access HTTP headers and cookies.
The servlet container creates an HttpServletResponse object and passes it as an argument to the servlet's service methods (doGet, doPost, etc).
Methods:

1.     addCookie

2.     encodeURL
3.     sendRedirect

addCookie

public void addCookie(Cookie cookie)
Adds the specified cookie to the response. This method can be called multiple times to set more than one cookie.
Parameters:
cookie - the Cookie to return to the client
encodeURL
public java.lang.String encodeURL(java.lang.String url)
Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. For example, if the browser supports cookies, or session tracking is turned off, URL encoding is unnecessary.
For robust session tracking, all URLs emitted by a servlet should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies.
Parameters:
url - the url to be encoded.
Returns:
the encoded URL if encoding is needed; the unchanged URL otherwise.
sendRedirect
public void sendRedirect(java.lang.String location)
                  throws java.io.IOException
Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
Parameters:
location - the redirect location URL
example program:
this program will describes working with Cookies.
public class Cookie
extends java.lang.Object
implements java.lang.Cloneable
Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
          
  <servlet>
  <servlet-name>gmr</servlet-name>
  <servlet-class>softech.CookiesExample</servlet-class>
   
  </servlet>
 
  <servlet-mapping>
  <servlet-name>gmr</servlet-name>
  <url-pattern>/first</url-pattern>
  </servlet-mapping>
   <servlet>
  <servlet-name>gmr2</servlet-name>
  <servlet-class>softech.GettingCookieInfo</servlet-class>

 
  </servlet>
 
  <servlet-mapping>
  <servlet-name>gmr2</servlet-name>
  <url-pattern>/second</url-pattern>
  </servlet-mapping>
  
 
 
</web-app>
CookiesExample.java
                    
package softech;

import java.io.PrintWriter;

import javax.servlet.http.*;

public class CookiesExample extends HttpServlet

{
      public void doGet(HttpServletRequest req,HttpServletResponse res)
      {
            try{
                 
           
      PrintWriter out=res.getWriter();
Cookie co=new Cookie("name","madhav");
res.setContentType("text/html");
res.addCookie(co);
out.println("cookies has been send to the browser");
      }
      catch(Exception e)
      {
            e.printStackTrace();
      }
      }
}

GettingCookieInfo.java
package softech;

import java.io.PrintWriter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GettingCookieInfo extends HttpServlet {

      public void doGet(HttpServletRequest req,HttpServletResponse res)
      {
            try
            {
                 
PrintWriter out=res.getWriter();
res.setContentType("text/html");
out.println("cookies info");
Cookie[] co=req.getCookies();
for (int i = 0; i < co.length; i++)
{
    out.println(co[i].getName());
    out.println(co[i].getValue());
}

      }
      catch(Exception e)
      {
            e.printStackTrace();
      }
      }
}

Example program:
//this program will describe working with HttpSession
public interface HttpSession
Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.
url:
http://madhav-4485e67f:8181/httpsession/form.html
Form.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>form.html</title>
     
  </head>
 
  <body>
    This is my HTML page. <br>
   
    <form action="first">
    name:<input type="text" name="name"><br>
    <input type="submit" value="send">
    </form>
  </body>
</html>

                   Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
          
  <servlet>
  <servlet-name>gmr</servlet-name>
  <servlet-class>control.HttpSessionExample</servlet-class>
  </servlet>
 
  <servlet-mapping>
  <servlet-name>gmr</servlet-name>
  <url-pattern>/first</url-pattern>
  </servlet-mapping>
   <servlet>
  <servlet-name>gmr2</servlet-name>
  <servlet-class>softech.MyServlet</servlet-class>
  </servlet>
 
  <servlet-mapping>
  <servlet-name>gmr2</servlet-name>
  <url-pattern>/second</url-pattern>
  </servlet-mapping>
 
 
</web-app>
HttpSessionExample.java:     
package control;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class HttpSessionExample extends HttpServlet {
                                    public void doGet(HttpServletRequest req,HttpServletResponse res)
                                    {
                                    try
                                    {
                                    String name=req.getParameter("name");        
                                    HttpSession session=req.getSession();
                                    session.setAttribute("sname","vegetables 2kg");
                                   
                                    res.setContentType("text");
                                    PrintWriter out=res.getWriter();
                                    out.println("hai madhav how r u");
                                    out.println("name="+name);
                                    String s=(String)session.getAttribute("sname");
                                    out.println("purchase items="+s);
                                    }
                                    catch(Exception e)
                                    {         
                                    }
                                    }
}

MyServlet.java
package softech;
import java.io.PrintWriter;
import javax.servlet.http.*;
public class MyServlet  extends  HttpServlet {
                public void doGet(HttpServletRequest req,HttpServletResponse res)
                {
                                try
                                {
                                //String name=req.getParameter("name");       
                                HttpSession session=req.getSession();
                                //session.setAttribute("sname", name);
                               
                                res.setContentType("text");
                                PrintWriter out=res.getWriter();
                                //out.println("hai madhav how r u");
                                //out.println("name="+name);
                                String s=(String)session.getAttribute("sname");
                                out.println("purcase items="+s);
                                }
                                catch(Exception e)
                                {             
                                }
                }
}

Example program:
//this program will describes maintain HttpSession by using URLRedirect techinique;


Form.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>form.html</title>
     

  </head>
 
  <body>
    This is my HTML page. <br>
   
    <form action="EncodeUrl.jsp">
    item to pruchase:<input type="text" name="name"><br>
    <input type="submit" value="send">
    </form>
  </body>
</html>

'EncodeUrl.jsp':                        
                <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'EncodeUrl.jsp' starting page</title>
   
  </head>

<%@ page session="true" %>
<%
String item=(String)request.getParameter("name");
session.setAttribute("sname",item);
String string=response.encodeURL("second");

 %>

 <a href='<%=string %>'> go to second url path</a>


                                     Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
      xmlns="http://java.sun.com/xml/ns/j2ee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
          
<servlet>
  <servlet-name>gmr2</servlet-name>
  <servlet-class>softech.MyServlet</servlet-class>
  </servlet>
 
  <servlet-mapping>
  <servlet-name>gmr2</servlet-name>
  <url-pattern>/second</url-pattern>
  </servlet-mapping>
 
 
</web-app>

MyServlet .java
package softech;
import java.io.PrintWriter;
import javax.servlet.http.*;
public class MyServlet  extends  HttpServlet {
                      public void doGet(HttpServletRequest req,HttpServletResponse res)
                      {
                                try
                                {
                                //String name=req.getParameter("name");       
                                HttpSession session=req.getSession();
                                //session.setAttribute("sname", name);
                               
                                res.setContentType("text");
                                PrintWriter out=res.getWriter();
                                //out.println("hai madhav how r u");
                                //out.println("name="+name);
                                String s=(String)session.getAttribute("sname");
                                out.println("purcase items="+s);
                                }
                                catch(Exception e)
                                {             
                                }
                      }
}


                                                                       

No comments:

Post a Comment