1.Run eclipse.File -> new -> dynamic web project
2. Give project name : example MyFirstApp and click on next
3. Please check Generate web.xml deployment descriptor to auto generate web.xml file
4.click on finish. now project structure will be created.
5.Go to webcontent and create a folder with name Jsp to place jsp files and create a index.jsp.
6. index.jsp will have an error because it is not having servlet jar. file so we need to include servler-apt.jar file.
now its fine. modify index.jsp with one text box and submit button.
- <%@ 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>Login</title>
-
- </head>
- <body>
-
- <form action="/MyFirstApp/hello" method="post">
-
- Name:<input type="text" name="name" value="">
- <input type="submit" name="name" value="submit">
-
- </form>
-
- </body>
-
- </html>
7. create a servlet
- package com.instanceofjava;
-
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class Hello extends HttpServlet{
-
- private static final long serialVersionUID = 1L;
-
- public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException,
- ServletException{
-
- String str= (String) req.getParameter("name");
- req.setAttribute("name", str);
-
- req.getRequestDispatcher("/Jsp/welcome.jsp").forward(req, res);
-
- }
- }
8. create a welcome.jsp page.
- <%@ 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>welcome</title>
- </head>
-
- <body>
-
- <%
- String str=null;
-
- try{
-
- str=(String)request.getAttribute("name");
-
- }catch(Exception e){
-
- }
-
- %>
-
- <p>Welcome: <%=str %></p>
-
- </body>
-
- </html>
8. go to we.xml and include our servlet
- <?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" xsi:schemaLocation="http://java.sun.com/xml/ns
- /javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
- <display-name>MyFirstApp</display-name>
- <welcome-file-list>
-
- <welcome-file>/Jsp/index.jsp</welcome-file>
-
- </welcome-file-list>
-
- <servlet>
-
- <servlet-name>HelloServlet</servlet-name>
- <servlet-class>com.instanceofjava.Hello</servlet-class>
-
- </servlet>
-
- <servlet-mapping>
-
- <servlet-name>HelloServlet</servlet-name>
- <url-pattern>/hello</url-pattern>
- </servlet-mapping>
-
- </web-app>
9. run the project , select tomcat 7 from apache.
10. Enter your name and click on submit.