The Artima Developer Community
Sponsored Link

Java Buzz Forum
Servlet example programs in eclipse

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Servlet example programs in eclipse Posted: Mar 15, 2015 7:05 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Servlet example programs in eclipse
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
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


Read: Servlet example programs in eclipse

Topic: Introduction to Spring profiles Previous Topic   Next Topic Topic: Integrating jOOQ with Grails Featuring the UWS-jOOQ Plugin

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use