我创建了dynamic web project
,并添加了两项:
index.jsp
这样的网页:
<%@ 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>
默认包 servlet
就像这样(名为GrettingServlet.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();
}
}
我安装了tomcat6
,因此我有Apache Software Foundation
个文件夹。
最后,我想创建此项目的war
文件,因此我选择了项目Export>War file
,并在Destination
文本中选择了路径{{1}中的webapps
文件夹}。
该项目名为C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps
。并且为了在服务器上看到MyFirstServlet
的形式,我在浏览器index.jsp
中写了但是我得到了消息
http://localhost:8080/MyFirstServlet/
servlet映射是这样的:
HTTP Status 404 - /MyFirstServlet/
type Status report
message /MyFirstServlet/
description The requested resource (/MyFirstServlet/) is not available.
Apache Tomcat/6.0.35
我在<?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>
可能是什么问题?
答案 0 :(得分:2)
从给定的示例中,您需要将Web应用程序部署到Tomcat中作为MyFirstServlet.war(或作为展开的目录 - 这没有区别)并将GrettingServlet映射到应用程序根 - 如果您希望servlet处理根目录:
您的/WEB-INF/web.xml
应该这样:
<servlet>
<servlet-name>GrettingServlet</servlet-name>
<servlet-class>your.package.GrettingServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>GrettingServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
请注意拼写错误:“Gretting”(在servlet和映射中)与“Greeting”(以JSP形式)
使用您的设置,您应该将浏览器指向http://localhost:8080/MyFirstServlet/GrettingServlet
以访问servlet。
如果您的想法是让JSP页面来处理根目录,那么您应该浏览http://localhost:8080/MyFirstServlet/<yourJSPName>.jsp
或者使用名为index.jsp
或default.jsp
的JSP(请参阅<welcome-file-list/>
你web.xml
的一部分。在这种情况下,我想,您的想法是显示JSP然后发布到servlet,因此请确保您的servlet规范和映射是正确的(web.xml
servlet映射和JSP表单action
属性)
答案 1 :(得分:1)
只需使用以下内容修改您的web.xml即可
<servlet>
<servlet-name>GrettingServlet</servlet-name>
<servlet-class>GrettingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GrettingServlet</servlet-name>
<url-pattern>/GreetingServlet</url-pattern>
</servlet-mapping>
更新这里是整个WEB.XML
<?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>/GreetingServlet</url-pattern>
</servlet-mapping>
</web-app>