还是先贴代码吧
1.User.java里面的代码如下所示:
package dto;
public class User {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
2.UserCheck里面的代码如下所示:
package service;
import dto.User;
public class UserCheck {
public boolean validate(User user) {
if (user != null && user.getName().equals("JSPMVC")) {
if (user.getPwd().equals("MVC")) {
return true;
}
return false;
}
return false;
}
}
3.LoginCheckServlet里面的代码如下所示:
package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 service.UserCheck;
import dto.User;
@WebServlet("/LoginCheckServlet")
public class LoginCheckServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("GBK");
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
User user = new User();
user.setName(name);
user.setPwd(pwd);
UserCheck uc = new UserCheck();
if (uc.validate(user)) {
request.setAttribute("user", user);
RequestDispatcher dis = request
.getRequestDispatcher("LoginSuccess.jsp");
dis.forward(request, response);
} else {
response.sendRedirect("LoginCheck.jsp");
}
}
}
4.LoginCheck.jsp里面的代码如下所示:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>LoginCheck.jsp</title>
</head>
<body>
<form action="LoginCheckServlet" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td><input type="submit" value="提交" /></td>
<td><input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
5.LoginSuccess.jsp里面的代码如下所示:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>LoginSuccess.jsp</title>
</head>
<body>
<jsp:useBean id="user" type="dto.User" scope="request" />
恭喜<jsp:getProperty property="name" name="user" />登录成功!
</body>
</html>
最开始服务器啥的都一切正常,运行就可以直接打开jsp,如下
点击提交就弹出500,,上网查了下说是空指针的问题,然后改了下,成功出现下一个错误(手动狗头)
这时候就是服务器部署的问题了,真的莫名其妙,代码啥的都没动过,服务器上一秒还能正常运行,下一次打开就出现这个错误了(想哭的心)
Artifact XXX:war exploded: Error during artifact deployment. See server log for details
接着再次上网查错
大概说的就是服务器的部署问题,然后去修改了tomcat中的Project Structure,发现都显示没错;还把war重新引用进去也没解决,并且日志里面啥都没有!!!
最后磨蹭了几个小时都没解决,万般无奈下重新创建了一个项目,结果就奇迹般得成功了。。。
真的想哭的心都有了,以后找到解决方法再来更新吧