@WebServlet("/login")
public class LoginController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding(“UTF-8”);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("");
stringBuilder.append(“登录名称:<input type=‘text’ name=‘loginName’ value’’>
”);
stringBuilder.append(“登录密码:
”);
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
showMsg(response,stringBuilder.toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");String loginName = req.getParameter("loginName");String loginPwd = req.getParameter("loginPwd");String msg=null;if("3187102442".equals(loginName)&& "1234".equals(loginPwd)) {msg = "你好,宝贝!"+loginName;}else{msg="用户名或密码错误!";}showMsg(resp, msg);
}private void showMsg(HttpServletResponse resp, String msg) throws IOException {resp.setCharacterEncoding("UTF-8");StringBuilder stringBuilder = new StringBuilder();stringBuilder.append("<html>");stringBuilder.append("<header>");stringBuilder.append("<title>Test</title>");stringBuilder.append("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">");stringBuilder.append("</header>");stringBuilder.append("<body>");stringBuilder.append(msg);stringBuilder.append("</body>");stringBuilder.append("</html>");PrintWriter out= resp.getWriter();out.println(stringBuilder.toString());out.close();}
}