赞
踩
jsp向servlet作请求,servlet接受到请求之后转发会原来的jsp页面,出现jsp页面不加载css样式的现象
1.jsp页面请求代码
- <script type="text/javascript">
- $(document).ready(function() {
- $("#submitbtn").click(function() {
- $("#loginform").attr("action","/BookStore/UserServlet?method=login")
- var uname = $("#username").val();
- var pwd = $("#password").val();
- if (uname == "") {
- alert("用户名不能为空");
- return false;
-
- }
- if (pwd == "") {
- alert("密码不能为空");
- return false;
- }
- $("#loginform").submit();
-
- });
- });
- </script>
2.servlet转发回页面代码
3.解决办法
public String login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User form = CommonUtils.toBean(req.getParameterMap(), User.class); Map<String, String> errors = new HashMap<String, String>(); // 对验证码进行校验 String sessionVerifyCode = (String) req.getSession().getAttribute("session_vcode"); String verifyCode = form.getVerifyCode();//等同于req.getParameter("verifyCode"); System.out.println("1."+sessionVerifyCode); System.out.println("2."+verifyCode); if (verifyCode == null || verifyCode.trim().isEmpty()) { errors.put("verifyCode", "验证码不能为空!"); } else if (verifyCode.length() != 4) { errors.put("verifyCode", "验证码长度必须为4!"); } else if (!verifyCode.equalsIgnoreCase(sessionVerifyCode)) { errors.put("verifyCode", "验证码错误!"); } /* * 判断map是否为空,不为空,说明存在错误 */ if (errors != null && errors.size() > 0) { /* * 1. 保存errors到request域 2. 保存form到request域,为了回显 3. 转发到regist.jsp */ req.setAttribute("errors", errors); req.setAttribute("user", form); return "f:/jsps/user/login.jsp"; } try { User user = userService.login(form); req.getSession().setAttribute("session_user", user); return "r:/index.jsp";// session重定向到index.jsp } catch (Exception e) { req.setAttribute("msg", e.getMessage()); req.setAttribute("form", form); return "f:/jsps/user/login.jsp";// 转发错误信息 } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。