我一直想它現在2天有時,Tomcat顯示錯誤500,再經過一段時間後,它會顯示錯誤404 我有我的目錄中列出 -JSP找不到我的servlet,有時提示錯誤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>
登記表登記-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();
}
}
我已經搜查幾乎無處不在,但我不能o解決我的問題。 在此先感謝! :)
[編輯]我展開組件看起來像這樣...
您的CustomerController是否在包中?如果是這樣,那麼你應該改變web.xml:PackageName.CustomerController。 –
@ItsikMauyhas - 他已經做到了。 ' com.loginpanel。web.CustomerController servlet-class>' –
GurV
@OP您正在使用Annotations和XML嗎?爲什麼? – GurV