Java html不使用servlet
我正在浏览java brains的jsp教程。本教程到此结束 我当前的问题是,当我运行我的html提交表单页面时,我会得到带有url的提交框Java html不使用servlet,java,html,jsp,servlets,Java,Html,Jsp,Servlets,我正在浏览java brains的jsp教程。本教程到此结束 我当前的问题是,当我运行我的html提交表单页面时,我会得到带有url的提交框 http://localhost:50373/DemoJSP/web/SimpleForm.html 当输入信息并单击提交时,它不会重定向到我的servlet页面。我得到了一个404,但没有找到这个url http://localhost:50373/DemoJSP/web/xmlServletpath?userName=testinfo 如果我输入一个h
http://localhost:50373/DemoJSP/web/SimpleForm.html
当输入信息并单击提交时,它不会重定向到我的servlet页面。我得到了一个404,但没有找到这个urlhttp://localhost:50373/DemoJSP/web/xmlServletpath?userName=testinfo
如果我输入一个http://localhost/xmlServletpath?userName=testinfo
我可以用Hello的输出点击servlet代码!testinfo
我在Intellij上运行tomcat
XmlServlet.java
@WebServlet(name = "XmlServlet")
public class XmlServlet 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();
String userName = request.getParameter("userName");
out.println("Hello!" + userName);
}
}
SimpleForm.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html" charset="UTF-8">
</head>
<body>
<form action="xmlServletpath">
<input name="userName"/>
<input type="submit"/>
</form>
</body>
</html>
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">
<!--<welcome-file-list>-->
<!--<welcome-file>index.html</welcome-file>-->
<!--</welcome-file-list>-->
<servlet>
<servlet-name>xmlServlet</servlet-name>
<servlet-class>org.nathan.javabrains.XmlServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xmlServlet</servlet-name>
<url-pattern>xmlServletpath</url-pattern>
</servlet-mapping>
</web-app>
xmlServlet
org.nathan.javabrains.XmlServlet
xmlServlet
xmlServletpath
项目结构
改变
<form action="xmlServletpath">
到
与
/
一样,它相对于网站根目录,而没有它则相对于当前URL。在web.xml
中,servlet的URL模式必须以斜杠开头:
<url-pattern>/xmlServletpath</url-pattern>
/xmlServletpath
我尝试了使用/和不使用/的情况,结果是一样的。根据Intellij项目结构,有效的URL应该是http://localhost:50373/SimpleForm.html
和http://localhost:50373/xmlServletpath?userName=testinfo
假设tomcat已更改为使用端口50373。另外,由于代码已经在使用@WebServlet
,最好只使用@WebServlet(name=“XmlServlet”,urlPatterns=“/xmlServletpath”)
,并从web.xml中删除servlet
和servlet映射
,以避免无意中被覆盖。这很有效!!!唯一的问题是我仍然不明白为什么我不能让xml文件工作。我经常看到这种xml配置,希望能够理解这个问题。无论如何,谢谢您的帮助。@WebServlet
仅从Servlet 3.0版开始介绍,作为xml配置的替代版本。Xml配置仍然受支持,您可以删除@WebServlet
注释,并将servlet
和servlet映射
添加回web.Xml
,程序也应能正常工作。一般来说,在同一个servlet中混合使用这两种方法不是一个好主意。当我删除@WebServlet注释并添加回servlet和servlet映射时,会出现相同的错误。找不到URL 404,但如果我键入它,它就会工作。再次感谢您的时间。在web.xml和SimpleForm.html中使用/和不使用/的组合
<url-pattern>/xmlServletpath</url-pattern>