简体   繁体   English

无法访问Java Servlet

[英]Can't access Java Servlet

I'm playing arround with JSP and Servlets and have the following problem: 我在与JSP和Servlet一起玩时遇到以下问题:

I have a JSP which implements something like a small "Login-Function". 我有一个JSP,它实现了类似小的“登录功能”的功能。 The code is the following: 代码如下:

<%@ 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>

As you can see i want to access the servlet LoginCheck. 如您所见,我要访问Servlet LoginCheck。 The code is the following: 代码如下:

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!");
        }
    }
}

If i use the code of the servlet in another JSP, everything works fine, but i am not able to access the servlet. 如果我在另一个JSP中使用Servlet的代码,则一切正常,但是我无法访问Servlet。 The XML ist the following: 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>

The server is a Jetty Server and no, i'm not allowed tu use a Tomcat. 该服务器是一台Jetty服务器,不,我不允许使用Tomcat。 The structure of my Project in Eclipse looks like this: http://imageshack.us/photo/my-images/204/eclipseo.png/ 我在Eclipse中的项目结构如下: http : //imageshack.us/photo/my-images/204/eclipseo.png/

Can you tell me why i can't access the servlet? 您能告诉我为什么我无法访问servlet吗?

Thanks! 谢谢!

Your <Servlet> has to be added in web.xml. 您的<Servlet>必须添加到web.xml中。 Eg 例如

<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>

You need to declare the servlet and the servlet mapping on your web.xml : 您需要在web.xml上声明servlet和servlet映射:

<servlet>
    <servlet-name>ServletName</servlet-name>
    <servlet-class>servlet.LoginCheck</servlet-class>
</servlet>

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

Also, If more than of your servlets share the common URL pattern, you must be careful about the order of their declaration in web.xml 另外,如果您的servlet中有多个共享公共URL模式,则必须注意它们在web.xml中的声明顺序。

For example: If you have two servlets named servlet1 and servlet2 and you want to invoke servlet1 on /something/something2 例如:如果您有两个名为servlet1和servlet2的servlet,并且要在/ something / something2上调用servlet1,

but mean while want to invoke servlet2 on /something/* 但意味着同时想在/ something / *上调用servlet2

than servlet1 should be declared before servlet2 in web.xml otherwise servlet2 will be invoked on every url pattern starting with /something/ 比servlet1应该在web.xml中的servlet2之前声明,否则servlet2将在以/ something /开头的每个URL模式上被调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM