I created dynamic web project , and add 2 items :
-
index.jsppage like this :<%@ 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>Insert title here</title> </head> <body> <form action="GrettingServlet" method="POST"> First Name: <input type="text" name="firstName" size="20"><br> Last Name: <input type="text" name="lastName" size="20"> <br><br> <input type="submit" value="Submit"> </form> </body> </html> -
in default package
servletlike this (calledGrettingServlet.java):import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class GrettingServlet extends HttpServlet { private static final long serialVersionUID = 1L; public GrettingServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String firstName = request.getParameter("firstName").toString(); String lastName = request.getParameter("lastName").toString(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet GreetingServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>Welcome " + firstName + " " + lastName + "</p>"); out.println("</body>"); out.println("</html>"); out.close(); } }
I installed tomcat6 so that I have Apache Software Foundation folder .
finally I want to create war file of this project, so I chose on the project Export>War file and in the Destination text I chose the webapps folder in the path C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps .
the project called MyFirstServlet . and in order to see the form of index.jsp on the server i write in the browser http://localhost:8080/MyFirstServlet/ but I get the message
HTTP Status 404 - /MyFirstServlet/
type Status report
message /MyFirstServlet/
description The requested resource (/MyFirstServlet/) is not available.
Apache Tomcat/6.0.35
the servlet mapping is this :
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MyFirstServlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>new</description>
<display-name>GrettingServlet</display-name>
<servlet-name>GrettingServlet</servlet-name>
<servlet-class>GrettingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GrettingServlet</servlet-name>
<url-pattern>/GrettingServlet</url-pattern>
</servlet-mapping>
</web-app>
I checked the tomcat and it on service status : started
What can be the problem ?
From the given example, you are expected to deploy your web application into your Tomcat as MyFirstServlet.war (or as an exploded directory – this makes no difference) and have your GrettingServlet mapped to the application root – if you want the servlet to handle the root:
Your
/WEB-INF/web.xmlshould have these so:Note the typo: “Gretting” (in servlet and mappings) vs “Greeting” (in JSP form)
With your setup you should be pointing your browser at
http://localhost:8080/MyFirstServlet/GrettingServletto reach the servlet.If your idea is to have JSP page to handle the root, then you should browse to either
http://localhost:8080/MyFirstServlet/<yourJSPName>.jspor have the JSP calledindex.jspordefault.jsp(see<welcome-file-list/>section of yourweb.xml). In this case your idea, I guess, is to display a JSP and then post to the servlet, therefore make sure your servlet specification and mapping is correct (web.xmlservlet mapping and the JSP formactionattribute).