简体   繁体   中英

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

I've been trying it for 2 days now, tomcat sometimes shows error 500, and then after some time, it shows error 404. I have my directory listed as -

The link to the image

The code for my web.xml is :

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

And my servlet's name is CustomerController .

The user-register.jsp includes the header, the form and the footer. **

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

The registration form registration-form.jsp is in the inc folder.

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>

And my servlet CustomerController looks like this -

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

}

I've searched almost everywhere, but i was not able to fix my issue. Thanks in advance ! :)

[Edit] My deployment assembly looks like this...

My web deployment assembly

Go to project properties and Find Deployment Assembly and then check whether you have folder that contain jsp files in here , if not add there. Mine look like 这个 .

I think i've figured out the problem.

The @WebServlet has to be after the @Resource annotation and removing the servlet tag from (web.xml) . But it's strange though that it prevented other projects in the same workspace to run too.

Thanks for the answers everyone! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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