簡體   English   中英

JSP無法找到我的servlet,有時它會給出錯誤500,有時會出現錯誤404

[英]JSP can't find my servlets, sometimes it gives error 500 and sometimes error 404

我已經嘗試了2天了,tomcat有時顯示錯誤500,然后一段時間后,它顯示錯誤404.我的目錄列為 -

圖像鏈接

我的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();
    }

}

我幾乎到處搜索,但我無法解決我的問題。 提前致謝 ! :)

[編輯]我的部署程序集看起來像這樣......

我的Web部署程序集

轉到項目屬性和查找部署程序集,然后檢查是否在此處包含包含jsp文件的文件夾,如果沒有在那里添加。 我看起來像 這個

我想我已經找到了問題所在。

@WebServlet必須在@Resource注釋之后並從(web.xml)中刪除servlet標記。 但奇怪的是,它阻止了同一工作區中的其他項目也運行。

謝謝大家的答案! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM