gpt4 book ai didi

java - 请求的资源在 Servlet、Tomcat7 中不可用

转载 作者:行者123 更新时间:2023-11-28 23:26:50 25 4
gpt4 key购买 nike

我知道这个问题已经被问过很多次并且也得到了回答。但是,我无法解决问题。我在 web.xml 中尝试了所有可能的映射方式。也使用注解@WebServlet。提交 html 表单后,我仍然无法转到我的 servlet。也尝试更改服务器位置。请帮忙。

Please find my web.xml, login page and servlet.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Webservice</display-name>
<servlet>

<servlet-name>MyServlet</servlet-name>
<servlet-class>controller.ItemServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/ItemServlet</url-pattern>
</servlet-mapping>


<welcome-file-list>
<welcome-file>html/Login.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>


<!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>Home Page</title>
<!-- <link rel="stylesheet" TYPE="text/css" href="../css/mystyle.css" ></link>
<link href='http://fonts.googleapis.com/css?family=Lily+Script+One' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="../js/Validations.js"></script>-->


</head>
<body>
<div id='header'>Online Music Store</div>

<div id='content'>
<form name="UserLogin" action="./ItemServlet?req=Login" method="post">
User Id <input type="text" name="userId" ></input>
Password<input type="password" name="password" ></input>
<input type="submit" value="Login"></input>
</form>

</div>

</body>
</html>


/**
* Servlet implementation class ItemServlet
*/
//@WebServlet("/ItemServlet")
public class ItemServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public ItemServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
doPost(request,response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

CDDao daoObj= new CDDao();
if("Login".equalsIgnoreCase(request.getParameter("req")))
{
System.out.println("Inside if");
System.out.println(request.getParameter("req"));

最佳答案

像这样更改您的 web.xml

<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>packagename.ItemServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/ItemServlet</url-pattern>
</servlet-mapping>

你的 servlet 类是

@WebServlet(name="MyServlet", urlPatterns={"/ItemServlet"})
public class ItemServlet extends HttpServlet {

重启服务器,检查是否有问题。另外,将此行更改为

<form name="UserLogin" action="ItemServlet" method="post">

关于java - 请求的资源在 Servlet、Tomcat7 中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35972462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com