Sunday, 23 September 2012

JSP - Servlets: Create your First JSP - Servlet Program using Eclipse IDE

This section is going to show you a step by step implement for a simple servlet example and run it. The example is going to:

Ask the user for a color in a JSP - in our example it will be "Home.jsp"
Display "Hello World" in the chosen color using a servlet - in our example it will be "helloWorld.java"


Steps



Create a new project
In the menu bar, File / new / Dynamic web project.
Name your project - to be consistent with the example, name it "ServletExample". This By choosing "Dynamic web project", eclipse creates the default folders hierarchy. The folders hierarchy is shown in the "Project Explorer" view at the left side of the eclipse window.If it is not shown, you can show it from the menu bar, Window / Show View / Project Explorer. It should look like this:







JSP, CSS files, images etc will be placed in the "WebContent" folder. Java files (Servlets) will be placed in the "Java Resources: src" folder.

Create the JSP file
In the "Project Explorer" view, R-click "WebContent" / New / JSP.
Name your JSP - to be consistent with the example, name it "Home.jsp"
place the JSP the following code:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> My first JSP </title> </head> <body> <form action="HelloServlet"> Please enter a color <br> <input type="text" name="color"size="20px"> <input type="submit" value="submit"> </form> </body> </html>

Create the Servlet
In the "Project Explorer" view, R-click "Java Resources: src" / New / Class
Name your class - to be consistent with the example, name it "HelloWorld"
place the following code in the class:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class HelloWorld extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// reading the user input
String color= request.getParameter("color");
PrintWriter out = response.getWriter();


out.println ( "<html> \n" + "<head> \n" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \n" + "<title> My first jsp </title> \n" + "</head> \n" + "<body> \n" + "<font size=\"12px\" color=\"" + color + "\">" + "Hello World" + "</font> \n" + "</body> \n" + "</html>" ); } }

Define your servlet in "web.xml"
Open web.xml from the "Project Explorer" view, WebContent / Web-INF / R-click web.xml / Open With / Text Editor
Replace its content by the following code:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" 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"> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/HelloServlet</url-pattern> </servlet-mapping> </web-app>

Add your project to Tomcat R-click the Tomcat server record / Add and Remove Projects; add your project


Open the "Servers" view by clicking the "Servers" tab at the bottom of eclipse window. If the "Servers" tab is not shown, you can show it from the menu bar, Window / Show View/ Servers. Or Window / Show View / OtherĂ¢€¦ / Server / Servers.

In the "Servers" view, R-click Tomcat-server record / Add and Remove Projects (If Tomcat-server record is not there, please check Steps 5-6 in the Configurations section by clicking next from here)

Add your project









Start "Tomcat": In the "Servers" view, R-click Tomcat-server record / Start
Test your project:
In your web browser (Internet Explorer or Firefox) :
If the port is set to 80: Type http://localhost/YourProjectName - In our example the URL is http://localhost/ServletExample

If the port is set to 8080: Type http://localhost:8080/YourProjectName - In our example the URL is http://localhost:8080/ServletExample Please note that Tomcat's default port is 8080, but the version refer to in the Downloads section (the pre-configured version), has its port set to 80. We will proceed in this tutorial assuming that the port is set to 80. (You can check your version's port from Tomcat-Installation-Directory / conf / server.xml)
Click your JSP file name shown in the directory listing - in our example it is Home.jsp







This should be the result












Set your project's welcome file (let the server open "Home.jsp" once you open the project)
Open "web.xml" (WebContent / Web-INF / R-click web.xml / Open With / Text Editor)
Set the "welcome-file-list" to be your home page - In our example it is Home.jsp. The code in "web.xml" should look like that:


<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" 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"> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/HelloServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>Home.jsp</welcome-file> </welcome-file-list> </web-app>


Restart the server and test your project again using http://localhost/yourProjectName - in our example, the URL will be http://localhost/ServletExample/.
Note that the server does not recognize changes in "web.xml" except if you restarted, or started, the server.


If you have passed these steps successfully; so Congratulations for running your servlet !!! To know how the process went in this example, click next.

Saturday, 15 September 2012

sendRedirect vs RequestDispatcher

sendRedirect vs RequestDispatcher
As you may already know that sendRedirect method in response object and RequestDispatcher object both are used to redirect to another page in servlets,but both works in a different fashion. RequestDispatcher forwards the request,response objects to another page/servlet this happens at the server side. where as sendRedirect method makes a new request from client side but client is unaware of the process.
for example if you say in real time if X is asking about some information to Y if Y is confirming with Z and telling you the answer is RequestDispatcher mechanism. or if Y is telling you that ask Z for that information then that is sendRedirect we can say(but client i.e X is unaware of the process since it is a automated flow of process that hapeens). By using RequestDispatcher the url in the client browser will be the first page address(i.e Y in example), in case of sendRedirect it changes to the second page address(i.e Z).