gpt4 book ai didi

java - 无法访问 Java Servlet

转载 作者:行者123 更新时间:2023-12-01 19:04:44 28 4
gpt4 key购买 nike

我正在使用 JSP 和 Servlet 并遇到以下问题:

我有一个 JSP,它实现了类似小型“登录功能”的功能。代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
</head>
<body>

Enter username and password:<br />
<hr />

<form method="GET" name="login" action="LoginCheck">
username<br />
<input type="text" name="loginName" /><br />
Password<br />
<input type="password" name="password" /><br />
<input type="submit" value="login" />
<input type="reset" value="reset" />
</form>

</body>
</html>

如您所见,我想访问 servlet LoginCheck。代码如下:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginCheck extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

String loginName = request.getParameter("loginName");
String password = request.getParameter("password");

if(loginName.equals("admin"))
{
if(password.equals("test"))
{
out.println("Username and password are correct!");
}else
{
out.println("Password were are incorrect!<br />");
out.println("<a href='index.jsp'>Here</a> you can get back to the Loginpage!");
}
}else
{
out.println("Username doesn't exist.<br />");
out.println("<a href='index.jsp'>Here</a> you can get back to the Loginpage!");
}
}
}

如果我在另一个 JSP 中使用 servlet 的代码,一切正常,但我无法访问该 servlet。 XML 如下:

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" 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>Servlet</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>

服务器是 Jetty 服务器,不,我不允许使用 Tomcat。我的 Eclipse 项目的结构如下所示: http://imageshack.us/photo/my-images/204/eclipseo.png/

你能告诉我为什么我无法访问 servlet 吗?

谢谢!

最佳答案

您的<Servlet>必须添加到 web.xml 中。例如

<servlet>
<servlet-name>builder</servlet-name>
<servlet-class>LoginCheck</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>builder</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

关于java - 无法访问 Java Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10533196/

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