需要注意的点有:1、html页面中form要有提交的action;2、web.xml要配置好,能让action链接过去。servlet那里比较容易理解,关键是看需要什么样的功能了,练习嘛,所以用了最简单的,顺便练练向浏览器写纠结的html语言,这样也许才能更加深刻理解为什么要用jsp。
login网页的body:
name: | |
password: |
这里,action="http://localhost:8080/welcome",说明我的servlet写好后要映射到welcome上的。
servlet中doPost的代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String userid = request.getParameter("userid");
String password = request.getParameter("password");
response.setContentType("text/html;charset=UTF-8");
out.println("" +
"
welcome"+"
Welcome " + userid + "" +"");
}
PS:不忍心一行一行的println()写了
最后,配置web.xml文件,关键部分是
forLogin
ksir.forLogin
forLogin
/welcome
我是在IDEA下做的。开启tomcat,打开login.html,输入用户名和密码(密码在这个例子里是个摆设……),点submit就跳转到welcome页面了!