栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Servlet与jsp相互转发重定向出现404和500错误

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Servlet与jsp相互转发重定向出现404和500错误

错误情况

当我们在项目中,实现页面挑战中,总是会遇到明明路径是匹配的,但是在实际操作中会报错
如

解决方法

如果你的电脑是自己的,绝壁会出现这样的错误,所以我们最好手动用contextPath读取项目路径
比如在jsp的form表单设置成

<%--
  Created by IntelliJ IDEA.
  User: chen Pen
  Date: 2022/5/17
  Time: 14:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


在servlet中

package com.javaweb;

import util.Constant;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class SServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        
        String contextPath =request.getContextPath();

        if (username.equals("admin")){
            request.getSession().setAttribute(Constant.USER_SESSION,request.getSession().getId());
            System.out.println("22222222");
            response.sendRedirect(contextPath+"/sys/sesssion.jsp");

        }
        response.sendRedirect(contextPath+"/error.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
    }
}

一般都能完美解决问题。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/914843.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号