1.创建severlet
2.next更改别名,后续如果修改可在xml中配置
3. 覆写service方法(由于这里没有学习JSP所以先放到一个service方法响应)
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置请求编码格式 req.setCharacterEncoding("utf-8"); //设置响应编码格式 resp.setContentType("text/html;charset=utf-8"); //获取请求信息 //处理请求信息 //响应处理结果 resp.getWriter().write("<html>"); resp.getWriter().write("<head>"); resp.getWriter().write("</head>"); resp.getWriter().write("<body>"); resp.getWriter().write("<form action='user' method='post'>"); resp.getWriter().write("用户名:<input type='text' name='uname' value=''/><br />"); resp.getWriter().write("密码:<input type='password' name='pwd' value=''/><br />"); resp.getWriter().write("<input type='submit' value='登录'/>"); resp.getWriter().write("</form>"); resp.getWriter().write("</body>"); resp.getWriter().write("</html>"); } }
4.修改项目名字
5.部署项目
方式一:
方式二:
6.启动项目进行访问(请求结束)
7.之后进行接收数据
resp.getWriter().write("<form action='user' method='post'>");
获取帐号密码:
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class UserServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置请求编码格式 req.setCharacterEncoding("utf-8"); //设置响应编码格式 resp.setContentType("text/html;charset=utf-8"); //获取请求信息 String uname=req.getParameter("uname"); String pwd=req.getParameter("pwd"); //处理请求信息 if("张三".equals(uname)&&"123".equals(pwd)){ resp.getWriter().write("登录成功");//响应处理结果 }else{ resp.getWriter().write("用户名或密码错误");//响应处理结果 } } }
8.验证
正确帐号密码:
错误帐号密码: