我想在屏幕上显示参数,使用以下网址: http://localhost:8080/HelloWorldServlet/hello.view?name=Jane http://localhost:8080/HelloWorldServlet/hello.jsp?name=Jane
但结果是“HTTP状态404 - 未找到”
这是servlet:
package controller;
import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet {
private static final String DEFNAME ="World";
protected void generateResponse
(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
if( name == null || name.length() == 0 )
name = DEFNAME;
out.println("Hello, "+name+"!");
out.close();
}
protected void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
generateResponse(request, response);
}
protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
generateResponse(request, response); }
}
jsp页面:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%! private static final String DEFNAME = "World"; %>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
String name = request.getParameter("name");
if( name == null || name.length() == 0 )
name = DEFNAME;
%>
<h2>Hello, <%= name %> !</h2>
</body>
</html>
xml:
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>controller.HelloWorldServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/hello.view</url-pattern>
</servlet-mapping>