package javapack;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class httpservlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");//解决向页面输出中文乱码,而且还可以写HTML
final long serialVersionUID=1L;
//接收表单参数
String username=request.getParameter("username");//通过input的name值获得输入的username
String password=request.getParameter("password");
//封装到实体对象
User user=new User();
user.setUsername(username);//将username与password封装到user对象中
user.setPassword(password);
//调用业务层处理数据
userservice us=new userservice();
try {
User existuser=us.login(user);//将user传入到login方法判断用户是否在数据库中存在
//根据处理结果显示信息(页面跳转)
if(existuser==null){
response.getWriter().println("<h1>登录失败</h1>");
}
else{
response.getWriter().println("<h1>登录成功,你好,"+existuser.getUsername()+"</h1></br>");
response.getWriter().println("<h1>5s后页面跳转。。。。</h1>");
response.setHeader("Refresh","5;url=1.html");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);//post方式和get方式执行同一方法
}
}
package javapack;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class httpservlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");//解决向页面输出中文乱码,而且还可以写HTML
final long serialVersionUID=1L;
//接收表单参数
String username=request.getParameter("username");//通过input的name值获得输入的username
String password=request.getParameter("password");
//封装到实体对象
User user=new User();
user.setUsername(username);//将username与password封装到user对象中
user.setPassword(password);
//调用业务层处理数据
userservice us=new userservice();
try {
User existuser=us.login(user);//将user传入到login方法判断用户是否在数据库中存在
//根据处理结果显示信息(页面跳转)
if(existuser==null){
response.getWriter().println("<h1>登录失败</h1>");
}
else{
/*response.getWriter().println("<h1>登录成功,你好,"+existuser.getUsername()+"</h1></br>");
response.getWriter().println("<h1>5s后页面跳转。。。。</h1>");
response.setHeader("Refresh","5;url=1.html");*/
response.setStatus(302);//状态码重定向
response.setHeader("Location", "success.html");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);//post方式和get方式执行同一方法
}
}