Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何映射servlet java_Java_Jsp_Servlets_Servlet Mapping - Fatal编程技术网

如何映射servlet java

如何映射servlet java,java,jsp,servlets,servlet-mapping,Java,Jsp,Servlets,Servlet Mapping,当前正在读取头优先Servlet Jsp。我目前一直在使用映射servlet。这可能是一个愚蠢的问题,但如何正确映射servlet url模式?我正在使用EclipseMars,这是我第一次使用jsp/servlet。我总是在创建动态web项目时检查生成web xml 这是默认web.xml中的内容 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchem

当前正在读取头优先Servlet Jsp。我目前一直在使用映射servlet。这可能是一个愚蠢的问题,但如何正确映射servlet url模式?我正在使用EclipseMars,这是我第一次使用jsp/servlet。我总是在创建动态web项目时检查生成web xml

这是默认web.xml中的内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>HeadFirst</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>
</web-app>
但是当我在显示名称下面添加这个时

<servlet>
    <servlet-name>Ch2Servlet</servlet-name>
    <servlet-class>com.test.hello</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Ch2Servlet</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
这是我的servlet类

@WebServlet("/Ch2Servlet")
public class Ch2Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        java.util.Date today = new java.util.Date();
        out.println("<h1>Hello World</h1>" + "<br>");
        out.println("Date today is " + today + "<br>");
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
@WebServlet(“/Ch2Servlet”)
公共类Ch2Servlet扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
PrintWriter out=response.getWriter();
java.util.Date today=new java.util.Date();
out.println(“你好世界”+“
”); out.println(“今天的日期是“+today+”
”; } 受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{ } }
顺便说一句,我读过这篇文章,但仍然感到困惑 编辑添加了我的serlvet类

url不是:

http://localhost:8080/HeadFirst/servlet/com.test.hello.Ch2Servlet
它将是:

http://localhost:8080/HeadFirst/HelloWorld
更具体地说,它将是您设置为
..

的值url不是:

http://localhost:8080/HeadFirst/servlet/com.test.hello.Ch2Servlet
它将是:

http://localhost:8080/HeadFirst/HelloWorld
更具体地说,它将是您设置为
..

的值url不是:

http://localhost:8080/HeadFirst/servlet/com.test.hello.Ch2Servlet
它将是:

http://localhost:8080/HeadFirst/HelloWorld
更具体地说,它将是您设置为
..

的值url不是:

http://localhost:8080/HeadFirst/servlet/com.test.hello.Ch2Servlet
它将是:

http://localhost:8080/HeadFirst/HelloWorld



更具体地说,它将是您设置为

的值,我在servlet的URL模式中看不到
/servlet
。当我单击run as时,eclipse会生成该值application@Onedaynerd不要盲目相信自动生成的资源,当您处于学习阶段时,情况就更糟了。@Onedaynerd如果您使用的是
Servlet 3.0
,您应该查看注释。仍然会出现404错误,让我编辑并发布我的代码,我无法在servlet的URL模式中看到
/servlet
,这是eclipse在单击RunAs时生成的application@Onedaynerd不要盲目相信自动生成的资源,当您处于学习阶段时,情况就更糟了。@Onedaynerd如果您使用的是
Servlet 3.0
,您应该查看注释。仍然会出现404错误,让我编辑并发布我的代码,我无法在servlet的URL模式中看到
/servlet
,这是eclipse在单击RunAs时生成的application@Onedaynerd不要盲目相信自动生成的资源,当您处于学习阶段时,情况就更糟了。@Onedaynerd如果您使用的是
Servlet 3.0
,您应该查看注释。仍然会出现404错误,让我编辑并发布我的代码,我无法在servlet的URL模式中看到
/servlet
,这是eclipse在单击RunAs时生成的application@Onedaynerd不要盲目相信自动生成的资源,如果你正在使用
Servlet 3.0
,你应该看看注释。仍然会出现404错误,让我编辑并发布我的代码。我看到你已经在使用注释了。要更改URL,只需更改
@WebServlet(“/Ch2Servlet”)的值即可
。我尝试更改注释,而此url现在可以在@WebServlet(“/HelloWorld”)下工作,因此注释会使url模式无效或过时。更改映射后,您需要重新启动服务器。是的,注释取代了更改映射的
web.xml
方法。因此,如果servlet版本3升级,建议只使用注释而不是映射?顺便说一句,当我删除注释时,我在安装servlet类时会出现一个错误Yes,使用注释设置路径更容易,您得到的错误可能是因为您在
web.xml
文件中设置了错误的
。我看到您已经在使用注释,要更改URL,只需更改
@WebServlet(“/Ch2Servlet”)
的值。我尝试更改注释,而此URL现在可以在@WebServlet(“/HelloWorld”)上运行,因此注释会使URL模式无效或过时。更改映射后,您需要重新启动服务器。是的,注释取代了更改映射的
web.xml
方法。因此,如果servlet版本3升级,建议只使用注释而不是映射?顺便说一句,当我删除注释时,我在安装servlet类时会出现一个错误Yes,使用注释设置路径更容易,您得到的错误可能是因为您在
web.xml
文件中设置了错误的
。我看到您已经在使用注释,要更改URL,只需更改
@WebServlet(“/Ch2Servlet”)
的值。我尝试更改注释,而此URL现在可以在@WebServlet(“/HelloWorld”)上运行,因此注释会使URL模式无效或过时。更改映射后,您需要重新启动服务器。是的,注释取代了更改映射的
web.xml
方法。因此,如果servlet版本3升级,建议只使用注释而不是映射?顺便说一句,当我删除注释时,我在安装servlet类时会出现一个错误Yes,使用注释设置路径更容易,您得到的错误可能是因为您在
web.xml
文件中设置了错误的
。我看到您已经在使用注释,要更改URL,只需更改
@WebServlet(“/Ch2Servlet”)
的值。我尝试更改注释,而此URL现在可以在@WebServlet(“/HelloWorld”)上运行,因此注释会使URL模式无效或过时。更改映射后,您需要重新启动服务器。是的,注释取代了更改映射的
web.xml
方法。因此,如果servlet版本3升级,建议只使用注释而不是映射?顺便说一句,当我删除注释时,我在安装servl时遇到了一个错误