总而言之,我是 Java Web 开发的新手,我正在尝试在我的测试中实现一个 Servlet。但是我发现我创建的 Servlet 不起作用。我不知道我是否错过了什么。请帮我检查一下。谢谢。
到目前为止我所做的是:
- 使用选项
创建了一个
Dynamic Web Project
命名。SecondWeb
Generate web.xml DD
- 在包下
添加了一个
Servlet
命名。我用值 和. 希望它适用于根目录下的所有 url 模式。HelloServlet
com.example.servlets
URL Mapping
/HelloServlet
/*
这是它的代码。
package com.example.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class for Servlet: HelloServlet
*
*/
public class HelloServlet extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().write("Hello, world!");
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
而且我还添加了一个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>My Title</title>
</head>
<body>
<% java.util.Date d = new java.util.Date(); %>
<h1>
Today's date is <%= d.toString() %> and this jsp page worked!
</h1>
</body>
</html>
我期望的是我希望在Hello world
访问URL或. 但似乎不起作用。为什么?谢谢。index.jsp
HTML
http://localhost:8080/SecondWeb
http://localhost:8080/SecondWeb/index.jsp
HelloServlet