Servlet跳转到jsp不加载css样式

来源:互联网 发布:jsp里写java代码 编辑:程序博客网 时间:2024/06/05 00:39

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转发回页面代码
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";// 转发错误信息}}
3.解决办法
将所有引用的css和js文件都写成绝对路径,如src="<%=path%>/css/......",path就是在开头声明一下<% String path=request.getContextPath(); %>。


0 0