三.测试
1.使用后台进行验证,在Test下建立class名为testlogin,使用debug验证
代码如下:
package Test;
import swsm.news.biz.NewsUserBiz;
import swsm.news.entity.NewsUser;
import swsm.news.iface.INewsUserBiz;
public class testlogin {
public static void main(String args[]){
INewsUserBiz ubiz = new NewsUserBiz();
NewsUser user = ubiz.doLogin("张三", "888888");
System.out.println(user);
}
}
2.使用jsp验证,在index.jsp下body内插入以下代码
<form action="${pageContext.request.contextPath}/LoginValidate" method="get">
<p>用户名:<input type='text' name='un' placeholder='请输入用户名' /></p>
<p>密码:<input type='password' name='pwd' placeholder ="密码"/></p>
<input type='submit' value='登陆'>
</form>
3.在swsm.news.control创建sevlet类名为:LoginValidate
代码为:
package swsm.news.control;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import swsm.news.biz.NewsUserBiz;
import swsm.news.entity.NewsUser;
import swsm.news.iface.INewsUserBiz;
public class LoginValidate extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = new String(request.getParameter("un").getBytes("ISO-8859-1"),"utf-8");
String password = request.getParameter("pwd");
System.out.println(username+password);
INewsUserBiz ubiz = new NewsUserBiz();
NewsUser user = ubiz.doLogin(username, password);
if(user!=null){
System.out.println("登录成功");
//response.sendRedirect("LoginSuccess.jsp");
}else{
System.out.println("登录失败");
}
}
}
4.部署web项目 在jsp页面输入用户名密码,然后再console看测试结果。