我最近尝试使用Tomcat和mySQL创建一个简单的登录页面,但每次运行代码时,其中一个servlet保持返回空白页面。这是servlet的代码。第一个来自登录servlet,是一个继续返回空白的那个:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class login extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection conn = null;
Statement statement = null;
try{
if(request.getParameter("username").isEmpty() || request.getParameter("pw").isEmpty()){
out.println("<html>\n" +
"<head><title>Error</title></head>\n" +
"<body>\n" +
"<a href = '/loginpage/'>Please fill in everything before you proceed</a>\n" +
"</body>\n" +
"</html>");
}else{
if(request.getParameter("username").length() < 50 && request.getParameter("pw").length() < 50){
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "javascrub", "xxxx");
statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("select * from userTable where username = " + "'" + request.getParameter("username") + "'");
if(!resultSet.isBeforeFirst()){
out.println("<html>\n" +
"<head><title>Error</title></head>\n" +
"<body>\n" +
"<a href = '/loginpage/'>Username does not exist, click to go back to login page</a><br>\n" +
"<a href = '/loginpage/signup.html'>Or click here to sign up</a>\n" +
"</body>\n" +
"</html>");
}else{
String pwCheck = null;
while(resultSet.next()){
pwCheck = resultSet.getString("password");
}
if(pwCheck.equals(request.getParameter("pw"))){
out.println("<html>\n" +
"<head><title>HomePage</title></head>\n" +
"<body>\n" +
"Congratulations " + request.getParameter("username") + ", you're logged in<br>\n" +
"<a href = '/loginpage/'>Log Out</a>\n" +
"</body>\n" +
"</html>");
}else{
out.println("<html>\n" +
"<head><title>Error</title></head>\n" +
"<body>\n" +
"<a href = '/loginpage/'>Incorrect password, click to go back</a>\n" +
"</body>\n" +
"</html>");
}
}
}else{
out.println("<html>\n" +
"<head><title>Error</title></head>\n" +
"<body>\n" +
"<a href = '/loginpage/'>Invalid username or password</a>\n" +
"</body>\n" +
"</html>");
}
}
}catch(SQLException ex){
ex.printStackTrace();
}finally{
out.println("<html>\n" +
"<head><title>Error</title></head>\n" +
"<body>\n" +
"<a href = '/loginpage/'>In finally block</a>\n" +
"</body>\n" +
"</html>");
out.close();
try{
if(statement != null){
statement.close();
}
if(conn != null){
conn.close();
}
}catch(SQLException ex){
ex.printStackTrace();
}
}
}
}
第二个是注册servlet。每当我将它放入/ classes文件夹并编译它时,登录servlet就会返回空白。
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class signup extends HttpServlet{
public void doPost(HttpServletRequest requestsu, HttpServletResponse responsesu) throws ServletException, IOException{
responsesu.setContentType("text/html");
PrintWriter outsu = responsesu.getWriter();
Connection connection = null;
Statement stmnt = null;
try{
if(requestsu.getParameter("usernamesu").isEmpty() || requestsu.getParameter("pwsu").isEmpty()){
outsu.println("<html>\n" +
"<head><title>Error</title></head>\n" +
"<body>\n" +
"<a href = '/loginpage/signup.html'>Please fill in everything before you proceed</a>\n" +
"</body>\n" +
"</html>");
}else{
if(requestsu.getParameter("usernamesu").length() < 50 && requestsu.getParameter("pwsu").length() < 50){
String pw = null;
pw = requestsu.getParameter("pwsu");
char[] pwArray = pw.toCharArray();
int letterCount = 0;
int numCount = 0;
for (int i = 0; i < pwArray.length; i++){
if(Character.isLetter(pwArray[i])){
letterCount++;
}else{
numCount++;
}
}
if (numCount >= 2 && letterCount >= 3){
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "javascrub", "xxxx");
stmnt = connection.createStatement();
ResultSet resultSet = stmnt.executeQuery("select * from userTable where username = " + "'" + requestsu.getParameter("usernamesu") + "'");
if(resultSet.isBeforeFirst()){
outsu.println("<html>\n" +
"<head><title>Error</title></head>\n" +
"<body>\n" +
"<a href = '/loginpage/signup.html'>We are sorry, this usernname is already taken</a>\n" +
"</body>\n" +
"</html>");
}else{
int countInserted = stmnt.executeUpdate("insert into userTable values ('" + requestsu.getParameter("usernamesu") + "', '" + requestsu.getParameter("pwsu") + "')");
outsu.println("<html>\n" +
"<head><title>Error</title></head>\n" +
"<body>\n" +
"<a href = '/loginpage/'>Congratulations, your account has been created, click to go back to the login page\n" +
"</body>\n" +
"</html>");
}
}else{
outsu.println("<html>\n" +
"<head><title>Error</title></head>\n" +
"<body>\n" +
"<a href = '/loginpage/signup.html'>Your password did not fulfull all the requirements, please try again</a>\n" +
"</body>\n" +
"</html>");
}
}else{
outsu.println("<html>\n" +
"<head><title>Error</title></head>\n" +
"<body>\n" +
"<a href = '/loginpage/signup'>Username and/or password too long</a>\n" +
"</body>\n" +
"</html>");
}
}
}catch(SQLException ex){
ex.printStackTrace();
}finally{
outsu.close();
try{
if(stmnt != null){
stmnt.close();
}
if(connection != null){
connection.close();
}
}catch(SQLException ex){
ex.printStackTrace();
}
}
}
}
无论登录servlet位于classes文件夹中,这个servlet都可以正常工作。我认为这个servlet可能会干扰登录servlet,但我不知道如何。 Web-xml文件可能有问题,所以我会在这里发布,但我不认为这是问题的根源。
这是:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>login</servlet-class>
<servlet-name>signup</servlet-name>
<servlet-class>signup</servlet-class>
</servlet>
<!-- Note: All <servlet> elements MUST be grouped together and
placed IN FRONT of the <servlet-mapping> elements -->
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
<servlet-name>signup</servlet-name>
<url-pattern>/signuppage</url-pattern>
</servlet-mapping>
</web-app>
我刚刚开始学习网页开发,我真的不太了解,所以任何帮助都会受到很大的赞赏。谢谢!
*如果有帮助,我也可以提供html页面
答案 0 :(得分:0)
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>login</servlet-class>
<servlet-name>signup</servlet-name>
<servlet-class>signup</servlet-class>
</servlet>
这没有任何意义。您需要两个<servlet>
元素:每个servlet一个。
<servlet-mapping>
相同。
不确定为什么您仍在使用XML,而不是简单地使用@WebServlet
注释您的课程。