java4all@1986 java. Powered by Blogger.

Example on sendRedirect Mechanism?

>> Tuesday, June 7, 2011

When we want that someone else should handle the response of our servlet, then there we should use send Redirect() method.
In send Redirect whenever the client makes any request it goes to the container, there the container decides whether the concerned servlet can handle the request or not.
If not then the servlet decides that the request can be handle by other servlet or jsp.
Then the servlet calls the send Redirect() method of the response object and sends back the response to the browser along with the status code.
Then the browser sees the status code and look for that servlet which can now handle the request.
Again the browser makes a new request, but with the name of that servlet which can now handle the request and the result will be displayed to you by the browser.
In all this process the client is unaware of the processing.
In this example we are going to make one html in which we will submit the user name and his password.
The controller will check if the password entered by the user is correct or not.
If the password entered by the user is correct then the servlet will redirect the request to the other servlet which will handle the request.
If the password entered by the user is wrong then the request will be forwarded to the html form.

HTML file:
<em><html>

<head>
<title>New Page 1</title>
</head>

<body>

<form method="POST" action="/SendRedirect/SendRedirectServlet">
<p>Enter your name��������
<input type="text" name="username" size="20"></p>
<p>Enter your password� <input type="text" name="password"
size="20"></p>
<p>����������
�� ���������
����������
��
<input type="submit" value="Submit" name="B1"></p>
</form>

</body>

</html>.</em>
SendRediectservlet Class
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SendRedirectServlet extends HttpServlet{
  protected void doPost(HttpServletRequest request, HttpServletResponse
    response)throws ServletException, IOException {
  response.setContentType("text/html");
  PrintWriter pw = response.getWriter();
  String name = request.getParameter("username");
  String password = request.getParameter("password");
  if(name.equals("James")&& password.equals("abc")){
  response.sendRedirect("/SendRedirect/ValidUserServlet");
  }
  else{
  pw.println("u r not a valid user");
  }
  }
} 
ValidUserServlet class 
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ValidUserServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse
  response)throws ServletException, IOException {
  PrintWriter pw = response.getWriter();
  pw.println("Welcome to roseindia.net " + " ");
  pw.println("how are you");
}
}
WEB.XML FILE:

 
 Zulfiqar
 SendRedirectServlet
 
 
 Zulfiqar
 /SendRedirectServlet
 
 
 Hello
 ValidUserServlet
 
 
 Hello
 /ValidUserServlet
 

Read more...

How to run a servlet?

To run a servlet one should follow the steps illustrated below:

   1.) Download and Install the tomcat server: Install the tomcat server in a directory in which you want to install and set the classpath.for the variable JAVA_HOME in the environment variable. To get details about the installation process and setting the classpath click the link Tomcat installation.

   2.) Set the class for the jar file: Set the classpath of the servlet-api.jar file in the variable CLASSPATH inside the environment variable by using the following steps.

    For Windows XP,

   3.) Go to Start->Control Panel->System->Advanced->Environment Variables->New button and Set the values as
    Variable Name:  CLASSPATH
    Variable Value:  C:\Program Files\Java\Tomcat 6.0\lib\servlet-api.jar

    For Windows 2000 and NT

   4.) Go to Start->Settings->Control Panel->System->Environment Variables->New button and Set the values as
    Variable Name:  CLASSPATH
    Variable Value:  C:\Program Files\Java\Tomcat 6.0\lib\servlet-api.jar

    Create a java source file and a web.xml file in a directory structure.

    Compile the java source file, put the compiled file (.class file) in the classes folder of your application and deploy the directory of your application in the webapps folder inside the tomcat directory.

    Start the tomcat server, open a browser window and type the URL http://localhost:8080/directory (folder name of your application) name/servler name and press enter.

    If everything is correct your servlet will run.

Read more...

Features of servlet 2.5?

>> Monday, June 6, 2011

This version has been released on September 26, 2005 by the Sun MicroSystems. It is not necessary that all web servers and application servers support the features of Servlet 2.5. Still most of the popular containers like Tomcat 5.5 and JBoss 4.0 support Servlet 2.4.

The list of the added features is given below:

   1.) Dependency on J2SE 5.0: The minimum platform requirement for Servlet 2.5 is JDK 1.5. Servet 2.5 can't be used in versions below than JDK1.5. All the available features of JDK1.5 like generics, autoboxing, an improved for loop etc are guaranteed available to Servlet 2.5 programmers.

   2.)  Support For annotations: Annotations provide a mechanism for decorating java code constructs (classes, methods, fields, etc.) with metadata information. Annotations are mark code in such a way that code processors may alter their behavior based on the metadata information.

   3.) Several web.xml convenience:
Servlet 2.5 introduces several small changes to the web.xml file to make it more convenient to use.  For example while writing a <filter-mapping>, we can now use an asterisk in a <servlet-name> which will represent all servlets as well as JSP.

    Previously we used to do

    <filter-mapping>
    <filter-name>FilterName</filter-name>
    <servlet-name>FilterName</servlet-name>
    </filter-mapping>

    Now,

    <filter-mapping>
    <filter-name>FilterName</filter-name>
    <servlet-name>*</servlet-name>
    </filter-mapping>
    
    Previously in <servlet-mapping> or <filter-mapping> there used to be only one <url-pattern>, but now we can have multiple <url-pattern>, like

    <servlet-mapping>
      <servlet-name>abc</servlet-name>
      <url-pattern>/abc/*</url-pattern>
      <url-pattern>/abc/*</url-pattern>
    </servlet-mapping>

    Apart from these changes, many more facilities added in web.xml.
   4.) A Handful of removed restrictions: Servlet 2.5 removed a few restrictions around error handling and session tracking. Now it has removed the restriction that the <error-page> could not call the setStatus() method to alter the error code that triggered them. In session tracking, Servlet 2.5 eased a rule that a servlet called by RequestDispatcher include() couldn't set response headers.

    5.)Some edge case clarifications: The servlet 2.4 specification says that before calling request.getReader() we  must call request.setCharacterEncoding().  However there is no such clarification given why it is so.

Read more...

Featurtes of servlet 2.4?

1.)In this tutorial you will learn the new features added in Servlet 2.4.

   2.) Upgraded supports for Http, J2SE, and J2EE: Servlet 2.4 depends on Http1.1 and J2SE 1.3.
    Additional ServletRequest methods : In Servlet 2.4 four new methods are added in the ServletRequest
        getRemotePort(): It returns the IP source port of the client.
        getLocalName(): It returns the host name on which the request was recieved.
        getLocalAddr(): It returns the IP address on which the request was recieved.
        getLocalPort(): It returns the IP port number.

    3.)New Support for Internationalization and charset choice: To provide support of internationization, Servlet 2.4 has added two new methods in the ServletResponse interface.
        setCharacterEncoding(String encoding): The purpose of this method is to set the response's character encoding.  This method helps us to pass a charset parameter to setContentType(String) or passing a Locale to setLocale(Locale). We can now avoid setting the charset in the setContentType("text/html;charset=UTF-8") as setCharacterEncoding() method pairs with the pre-existing getCharacterEncoding() method to manipulate and view the response's character encoding.
        
        4.)getContentType(): It is responsible for returning the response's content type. The content type can be dynamically set with a combination of setContentType(), setLocale(), and setCharacterEncoding() calls, and the method getContentType() provides a way to view the generated type string.

   5.)New features has been added in RequestDispatcher: In Servlet 2.4 five new request attributes has been added for providing extra information during a RequestDispatcher forward() call. This features has been added is Servlet 2.4 to know the true original request URI. The following request attributes are:
        javax.servlet.forward.request_uri
        javax.servlet.forward.context_path
        javax.servlet.forward.servlet_path
        javax.servlet.forward.path_info
        javax.servlet.forward.query_string

 6.)SingleThreadModel interface has been deprecated: In Servlet 2.4 the SingleThreadModel interface has been deprecated.

7.)HttpSession details and interaction with logins has been clarified: The new method HttpSession.logout() has been added in Servlet 2.4. Now session allows zero or negative values in the <session-timeout> element to indicate sessions should never time out.
    If the object in the session can't be serialize in a distributed environment then it must throw an IllegalArgumentException.

    7.)Welcome file behavior and Classloading has been clarified:
In servlet 2.4 welcome file can be a servlet.

    8.)The web.xml file now uses XML Schema: Version 2.4 servers must still accept the 2.2 and 2.3 deployment descriptor formats, but all new elements are solely specified in Schema.

Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP