1.Run eclipse.File -> new -> dynamic web project

Servlet Example in exclipse


2. Give project name : example MyFirstApp and click on next



Servlet Example in exclipse


3. Please check Generate web.xml deployment descriptor to auto generate web.xml file


Servlet Example in exclipse




interview Servlet Example in exclipse

4.click on finish. now project structure will be created.

Servlet Example program  in exclipse

5.Go to webcontent and create a folder with name Jsp to place jsp files and create a index.jsp.


Servlet Example program  in exclipse




Servlet Example program  in exclipse


Servlet Example program  in exclipse





Servlet Example program  in exclipse


6. index.jsp will have an error because it is not having servlet jar. file so we need to include servler-apt.jar file.

Servlet Example program  in exclipse


Servlet Example program  in exclipse



Servlet Example program  in exclipse



now its fine. modify index.jsp with one text box and submit button.


Servlet Example program  in exclipse




  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org
  4. /TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8.  
  9. <title>Login</title>
  10.  
  11. </head>
  12. <body>
  13.  
  14. <form action="/MyFirstApp/hello" method="post">
  15.  
  16.        Name:<input type="text" name="name" value="">
  17.         <input type="submit" name="name" value="submit">
  18.  
  19.  </form>
  20.  
  21. </body>
  22.  
  23. </html>


7. create a servlet


Servlet Example program  in exclipse






Servlet Example program  in exclipse


Servlet Example program  in exclipse


  1. package com.instanceofjava;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8.  
  9. public class Hello extends HttpServlet{
  10.  
  11.   private static final long serialVersionUID = 1L;
  12.  
  13. public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException,
  14. ServletException{
  15.  
  16.         String str= (String) req.getParameter("name");
  17.         req.setAttribute("name", str);
  18.  
  19.         req.getRequestDispatcher("/Jsp/welcome.jsp").forward(req, res);
  20.  
  21.     }
  22. }

8. create a welcome.jsp page.


Servlet Example program  in exclipse


Servlet Example program  in exclipse








  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
  2. pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org
  4. /TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8.  
  9. <title>welcome</title>
  10. </head>
  11.  
  12. <body>
  13.  
  14. <%
  15. String str=null;
  16.  
  17. try{
  18.  
  19.     str=(String)request.getAttribute("name");
  20.  
  21. }catch(Exception e){
  22.  
  23. }
  24.  
  25. %>
  26.  
  27. <p>Welcome: <%=str %></p>
  28.  
  29. </body>
  30.  
  31. </html>


8. go to we.xml and include our servlet




Servlet Example program  in exclipse



  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns
  4. /javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  5.  <display-name>MyFirstApp</display-name>
  6.  <welcome-file-list>
  7.  
  8.     <welcome-file>/Jsp/index.jsp</welcome-file>
  9.  
  10.   </welcome-file-list>
  11.  
  12.   <servlet>  
  13.  
  14.    <servlet-name>HelloServlet</servlet-name>  
  15.    <servlet-class>com.instanceofjava.Hello</servlet-class> 
  16.  
  17.   </servlet>  
  18.  
  19. <servlet-mapping>
  20.  
  21.         <servlet-name>HelloServlet</servlet-name>
  22.         <url-pattern>/hello</url-pattern>
  23.     </servlet-mapping>
  24.  
  25. </web-app>


9. run the project , select tomcat 7 from apache.

Servlet Example program  in exclipse


10. Enter your name and click on submit.

Servlet Example program  in exclipse


Servlet Example program  in exclipse


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu