PrintNamesServlet.java
此servlet打印输入的用户名
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "PrintNamesServlet")
public class PrintNamesServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Ram Dhakal");
}
}
CounterServlet.java
计算页面中的点击次数或访问次数
import javax.servlet.ServletException; import
> javax.servlet.annotation.WebServlet; import
> javax.servlet.http.HttpServlet; import
> javax.servlet.http.HttpServletRequest; import
> javax.servlet.http.HttpServletResponse; import java.io.IOException;
> import java.io.PrintWriter;
>
> @WebServlet(name = "CounterServlet") public class CounterServlet
> extends HttpServlet {
> int totalHits;
> public void init() throws ServletException{
> totalHits = 0;
> }
> protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
> PrintWriter out = response.getWriter();
> out.print("Total visit count: " + totalHits++);
> } public void destroy(){
> }
> }
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>PrintNamesServlet</servlet-name>
<servlet-class>PrintNamesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PrintNamesServlet</servlet-name>
<url-pattern>/PrintNamesServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>CounterServlet</servlet-name>
<servlet-class>CounterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CounterServlet</servlet-name>
<url-pattern>/CounterServlet</url-pattern>
</servlet-mapping>
</web-app>
我收到错误:找不到网址的网页:http://localhost:8080/ 因为,我试图第一次运行servlet,我没有得到我的代码有什么问题。我在网址中输入了http://localhost:8080/PrintNamesServlet。
答案 0 :(得分:0)
使用注释@WebServlet(name = "PrintNamesServlet") the
时,不使用web.xml`映射。
您必须删除这些注释或向其添加urlMapping
属性。
答案 1 :(得分:0)
您使用过Servlet 3.0规范 让我们来看看它
在Servlet 3.0中,可以使用@WebServlet
指定servlet元数据@WebServlet(name="mytest",
urlPatterns={"/myurl"})
public class TestServlet extends javax.servlet.http.HttpServlet {
....
}
通过这种方式,可以使用注释中指定的url模式访问servlet。
@@WebServlet(name="mytest",urlPatterns={"/myurl"})
根据该servlet使用
加入 http://localhost:8080/myurl
在您的情况下,您只需要指定名称,您也必须指定urlPatterns,这样您就可以正确地调用servlet。
@WebServlet(name = "CounterServlet",urlPatterns={"/CounterServlet"}) public class CounterServlet extends HttpServlet {}
您不需要使用web.xml文件。