JSP无法找到我的servlet,有时会出现错误500,有时会出现错误404

时间:2017-05-07 13:40:02

标签: java jsp tomcat servlets

我现在已经尝试了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 包括标题,表单和页脚。 **

  • 用户register.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文件夹中。

登记-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 看起来像这样 -

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

1 个答案:

答案 0 :(得分:0)

我想我已经找到了问题。

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

感谢大家的答案! :)