【问题标题】:JSP can't find my servlets, sometimes it gives error 500 and sometimes error 404JSP 找不到我的 servlet,有时它会给出错误 500,有时会给出错误 404
【发布时间】:2017-05-07 13:40:01
【问题描述】:

我已经尝试了2天,tomcat有时会显示错误500,然后过一段时间,它会显示错误404。 我的目录列为 -

The link to the image

我的 web.xml 的代码是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">



<servlet>
    <servlet-name>CustomerController</servlet-name>
    <servlet-class>com.loginpanel.web.CustomerController</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>CustomerController</servlet-name>
    <url-pattern>/CustomerController</url-pattern>
</servlet-mapping>


<welcome-file-list>
    <welcome-file>CustomerController</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

而我的 servlet 的名称CustomerController

user-register.jsp 包括页眉、表单和页脚。 **

  • 用户注册.jsp

**

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>

    <head>
        <title>Registration form</title>
    </head>

    <body style="background-color:#55acee;">

        <!-- include the header page -->
        <%@include file="../inc/header.jsp" %>

        <div class="container">
            <div class="row">
                <div class="col-sm-6 col-sm-offset-3">

                    <!-- include the form -->
                    <%@include file="../inc/registration-form.jsp" %>

                </div><!-- col-sm-6 -->
            </div><!-- row -->
        </div><!-- container -->

    </body>

</html>

注册表registration-form.jsp在inc文件夹中。

registration-form.jsp

    <form action="/CustomerController" method="POST" id="registration-form">

    <header><h1 class="font-light">Register</h1></header><hr>

    <input type="hidden" name="command" value="ADD" />

    <p><label>First name</label>
    <input type="text" required name="firstName" class="form-control" placeholder="First name" /></p>

    <p><label>Last name</label>
    <input type="text" required name="lastName" class="form-control" placeholder="Last name" /></p>

    <p>
        <label>Country</label>
        <select name="country" class="form-control">
            <option>Australia</option>
            <option>Brazil</option>
            <option>Denmark</option>
            <option>France</option>
            <option selected >India</option>
            <option>Spain</option>
            <option>UK</option>
            <option>USA</option>
        </select>
    </p>

    <p><label>Username</label>
    <input type="text" required name="username" class="form-control" placeholder="Username" /></p>

    <div class="row">
        <div class="col-sm-6">
            <p><label>Password</label>
            <input type="password" id="pass" required name="password" class="form-control" placeholder="Password" minlength="8" /></p><br>
        </div>

        <div class="col-sm-6">
            <p><label>Confirm password</label>
            <input type="password" id="cpass" required name="confirmPassword" class="form-control" placeholder="Confirm Password" minlength="8" /></p><br>
        </div>
    </div>

    <input type="submit" value="Register" onclick="if(checkPass()){ return true; } else { alert('The passwords do not match'); return false; }" class="btn btn-success" /> ( Registered users may <a href="./">Login here</a> )

</form>


<script language="javascript">
    function checkPass() {
        var pass = document.getElementById("pass").value;
        var cpass = document.getElementById("cpass").value;
        if(pass==cpass && (pass!=''|| cpass!='')){
            return true;
        } else {
            return false;
        }
    }
</script>

而我的 servlet CustomerController 看起来像这样 -

客户控制器

package com.loginpanel.web;

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

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.*;
import javax.sql.DataSource;

/**
 * Servlet implementation class CustomerController
 */
@WebServlet("/CustomerController")

public class CustomerController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private CustomerDbUtil customerDbUtil;
    DataSource dataSource;



    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            // Get the command from the form
            String command = request.getParameter("command");

            // Route the flow accordingly
            switch(command) {
                case "ADD":
                    addCustomer(request, response);
                break;
            }
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

    private void addCustomer(HttpServletRequest request, HttpServletResponse response) throws Exception {

        try {
            // Get the form values
            String firstName = request.getParameter("firstName");
            String lastName = request.getParameter("lastName");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String country  = request.getParameter("country");

            // Create the Customer class object
            Customer theCustomer = new Customer(firstName, lastName, username, password, country);

            // Call the addCustomer function of the model
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println(theCustomer.getMd5());

            customerDbUtil.addCustomer(theCustomer);            

        } catch(Exception e){
            e.printStackTrace();
        } finally {
            // Redirect to the login page
            //response.sendRedirect("index.jsp");
        }   
    }


    // Hash the string to MD5
    private String md5(String str) throws Exception {

        String plainText = str;
        StringBuffer hexString = new StringBuffer();

        if(plainText!=null) {
            MessageDigest mdAlgorithm = MessageDigest.getInstance("MD5");
            mdAlgorithm.update(plainText.getBytes());

            byte[] digest = mdAlgorithm.digest();

            for (int i = 0; i < digest.length; i++) {
                plainText = Integer.toHexString(0xFF & digest[i]);

                if (plainText.length() < 2) {
                    plainText = "0" + plainText;
                }

                hexString.append(plainText);
            }
        }
        return hexString.toString();
    }

}

我几乎在所有地方都进行了搜索,但我无法解决我的问题。 提前致谢 ! :)

[编辑] 我的部署程序集如下所示...

My web deployment assembly

【问题讨论】:

  • 您的 CustomerController 是否在一个包中?如果是这样,那么您应该更改 web.xml : PackageName.CustomerController.
  • @ItsikMauyhas - 他已经做到了。 &lt;servlet-class&gt;com.loginpanel.web.CustomerController&lt;/servlet-class&gt;
  • @OP 您同时使用注解和 XML?为什么?
  • 是的,我已经把它放在了一个包裹里。名称是“com.loginpanel.web”.. 包含我的 CustomerController servlet。感谢您的回复:)
  • 您已经定义了两次 - @WebServlet("/CustomerController") 并在您的 web.xml 中选择一个。

标签: java jsp tomcat servlets


【解决方案1】:

转到项目属性并找到部署程序集,然后检查此处是否有包含 jsp 文件的文件夹,如果没有添加。 我的看起来像

【讨论】:

  • @parlad..我已经用 snap 更新了帖子。我想我已经添加了类文件夹。请帮帮我。谢谢
  • 根据您的更新帖子,一切对我来说都很好,尝试刷新并清理项目并重新启动您的网络服务器。
  • 也试过了...事实上我已经重新安装了几次jre、eclipse和tomcat。仍然没有运气。感谢您的回复:)
【解决方案2】:

我想我已经解决了问题。

@WebServlet 必须在 @Resource 注释之后并从 (web.xml) 中删除 servlet 标记。但奇怪的是,它阻止了同一工作区中的其他项目也运行。

感谢大家的回答! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-07
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多