学习中的owl 2020-03-26 23:05 采纳率: 85.7%
浏览 341
已采纳

jsp跳转servlet问题 当用注解的时候报404,用配置web.xml时报500,两种方法没有同时进行

百度了相关问题
注解时用的是value属性

用的是tomcat10
可以访问login页面

RedirectServletServle

package com.lxy;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login1")
//@WebServlet(name = "login ",urlPatterns = "/login1")
public class RedirectServletServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
//        String name = request.getParameter("username");
//        String word = request.getParameter("password");
//        request.setAttribute("n", name);
        try {
            ServletOutputStream out = response.getOutputStream();
            out.print("内容");
            out.println("<html><head><title></title></head><body>heelo world!!!</body></html>");
        } catch (IOException e) {
            e.printStackTrace();

        }
//        if(name.equals("lxy")&&word.equals("123456")){
//            response.sendRedirect("weclome.jsp");//重定向“/”表示根目录,绝对路径
//        }else {
//            response.sendRedirect("error.jsp");//相对路径
//        }
    }
}

图片说明

目录情况

login.jsp

<%@ page pageEncoding="UTF-8" import="java.util.*" language="java" %>
<% String path=request.getContextPath();%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>用户登录</title>
</head>
<body>
<center>
    <p>用户登录</p>
</center>
<form action="login1" method="post">
    <table border="1" width="250px;" align="center">
        <tr>
            <td width="75px">用户名:</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td width="75px">密&nbsp;&nbsp;码:</td>
            <td><input name="pwd" type="password"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="登录"/>&nbsp;&nbsp;
            </td>
        </tr>
    </table>
</form>
</body>
</html>


图片说明图片说明
图片说明

用注解的时候tomcat的日志信息报404错

图片说明图片说明

用web.xml注解是报的500(可能是servlet-class的问题)

这是web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
    <servlet-name>ok</servlet-name>
    <servlet-class>com.lxy.RedirectServletServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ok</servlet-name>
        <url-pattern>/login1</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>
  • 写回答

1条回答 默认 最新

  • 关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。