servlet配置文件web.xml中<url-pattern>的问题?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了servlet配置文件web.xml中<url-pattern>的问题?相关的知识,希望对你有一定的参考价值。
项目名:ServletDemo
servlet文件:
package com.servlet.session.Demo2;
public class LoginServlet extends HttpServlet
public LoginServlet()
super();
public void destory()
super.destroy();
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
doPost(request,response);
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
String username = request.getParameter("username");
String passwd = request.getParameter("passwd");
if(username.equals("jb-aptech")&&passwd.equals("12345"))
HttpSession session= request.getSession(true);
session.setAttribute("username", username);
session.setAttribute("passwd", passwd);
RequestDispatcher view = request.getRequestDispatcher("InfoServlet");
view.forward(request, response);
else
response.sendRedirect("../login.jsp");
public void init() throws ServletException
jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<! DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</title>
<script type="text/javascript">
function RegisterSubmit()
with(document.Register)
var loginname= username.value;
var loginpasswd= passwd.value;
if(loginname==null|| loginname=="")
alert("请填写用户名");
else if(loginpasswd==null|| loginpasswd=="")
alert("请填写密码");
else
document.Register.submit();
</script>
</head>
<body>
<from action="servlet/LofinServlet" name="Register" method="post">
<center>
<table width="600" borfer="0" bacolor="#8FBC8F">
<tr>
<td align="right">用户名:</td>
<td align="left"><input type="text" name="username" style="widty:120px"/></td>
</tr>
<tr>
<td align="right">密 码:</td>
<td align="left"><input type="password" name="passwd" style="widty:120px"/></td>
</tr>
<tr>
<td align="right"><input type="button" value="提交" onClisk="RegisterSubmit()"> </td>
<td align="left"><input type="reset" value="重置" /></td>
</tr>
</table>
</center>
</body>
</html>
web.xml文件:
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.servlet.session.Demo2.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/LoginServlet</url-pattern>
</servlet-mapping>
问题:启动tomcat后,在网页中键入http://localhost:8080时,正常。但是再键入http://localhost:8080/ServlerDemo/servlet/LoginServlet 时, 怎么出现404错误。是不是我的地址输入错误,正确的地址是哪样的。
怎么我改了之后,还是同样的问题。
写错了应该是f--->g :LoginServlet
工程里面的webinfo---》改名为WEB-INF。重新部署试试吧
你的地址输入应该是正确的。追问
怎么我改了之后,还是同样的问题?
追答兄弟,你的应用名字是ServletDemo。
所以你的URL应该是http://localhost:8080/ServletDemo/servlet/LoginServlet
其中你又拼写错了。
所以应该键入http://localhost:8080/servlet/LoginServlet 参考技术C 把<url-pattern>/servlet/LoginServlet</url-pattern> 里面改成<url-pattern>/LoginServlet</url-pattern>
,jsp页面action项也改成action=“LoginServlet” 参考技术D WebInfo没见过 第5个回答 2020-04-01 <from
action="servlet/LofinServlet"
name="Register"
method="post">
写错了应该是f--->g
:LoginServlet
工程里面的webinfo---》改名为WEB-INF。重新部署试试吧
你的地址输入应该是正确的。
web.xml
在Tomcat/conf下有一个web.xml文件,当Tomcat中所有web应用实例化时都会读取该配置文件,然后读取每个web应用自己的配置文件(/WEB-INF/web.xml)。
conf/web.xml作出以下配置:
//处理未映射到其他servlet的请求 <servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> //jsp页面编译和执行servlet <servlet> <servlet-name>jsp</servlet-name> <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> <init-param> <param-name>fork</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>xpoweredBy</param-name> <param-value>false</param-value> </init-param> <load-on-startup>3</load-on-startup> </servlet> //映射关系 <!-- The mapping for the default servlet --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- The mappings for the JSP servlet --> <servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*.jsp</url-pattern> <url-pattern>*.jspx</url-pattern> </servlet-mapping> //会话设置 <session-config> <session-timeout>30</session-timeout> </session-config> //MIME类型映射 <mime-mapping> <extension>123</extension> <mime-type>application/vnd.lotus-1-2-3</mime-type> </mime-mapping> <mime-mapping> <extension>3dml</extension> <mime-type>text/vnd.in3d.3dml</mime-type> </mime-mapping> <mime-mapping> <extension>3ds</extension> <mime-type>image/x-3ds</mime-type> </mime-mapping> <mime-mapping> <extension>3g2</extension> <mime-type>video/3gpp2</mime-type> </mime-mapping> ... ... ...
以上是关于servlet配置文件web.xml中<url-pattern>的问题?的主要内容,如果未能解决你的问题,请参考以下文章
JSP中站点中的WEB-INF下的web.xml文件有啥用呢?一般写啥内容的?
创建Servlet类,并重写Servlet接口,配置web.xml文件,实现Servlet访问
servlet配置文件web.xml中<url-pattern>的问题?