第一种
通过超链接的方式发送请求
jsp页面代码:
<a href="preregister" color=blue size="20">注册用户</a>
controller控制器中的代码:
@RequestMapping("/preregister")
public String preregister() {
return "register/userRegister";
}
通过注解的方式调用controller中的方法完成请求响应。
第二种
通过jsp中表格form提交的中的action属性发送请求:
jsp代码:
<form action="login" method="post">
<table align="center" border="1">
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登录" name="login"></td>
<td><input type="reset" value="重置" name="reset"></td>
</tr>
</table>
</form>
controller代码:
@RequestMapping("/login")
public String userlogin(@RequestParam(value="username")String username,@RequestParam(value="password")String password,HttpServletResponse response,HttpServletRequest request,Model model) throws IOException {
PrintWriter out = null;
HttpSession session=request.getSession();
User user=userService.findUserByid(username);
if(user!=null&&user.getPassword().equals(password)) {
session.setAttribute("username", username);//登录成功则在session对象中添加用户名信息
response.setContentType("text/html; charset=UTF-8"); //转码
try {
out = response.getWriter();
out.flush();
out.println("<script>");
out.println("alert('登陆成功!');");
out.println("</script>");
return "index/systemindex";
} catch (IOException e) {
e.printStackTrace();
}
}else {
response.setContentType("text/html; charset=UTF-8"); //转码
try {
out = response.getWriter();
out.flush();
out.println("<script>");
out.println("alert('用户名或者密码错误!');");
out.println("history.back();");
out.println("</script>");
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
out.close();
}
}
return null;
}
也是通过注解的方式响应由jsp发来的请求。

1570

被折叠的 条评论
为什么被折叠?



