最近在學前端開發,對于使用sendRedirect跳轉頁面總是出現問題。
這是我的檔案夾框架結構:

Servletdemo1的代碼如下:
package com.wx.servlet;
import javax.servlet.ServletException;
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(name = "Servletdemo1")
public class Servletdemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//重定向
response.sendRedirect("/jspdemo1/demo1.jsp");
}
}
jsp頁面代碼如下:
<%--
Created by IntelliJ IDEA.
User: W
Date: 2020/6/11
Time: 8:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Jspdemo1</title>
</head>
<body>
<hl>跳轉后頁面!</hl>
</body>
</html>
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>servlet1</servlet-name>
<servlet-class>com.wx.servlet.Servletdemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/servletdemo1</url-pattern>
</servlet-mapping>
</web-app>
Tomcat的配置如下:


運行服務器:

輸入地址servletdemo1之后,頁面跳轉,出現404

有沒有大佬知道怎么回事啊?求幫忙
uj5u.com熱心網友回復:
別沉啊!!!uj5u.com熱心網友回復:
首先你要確認你的專案在tomcat的webapps是撒名。例如:demo. 哪么你的訪問地址為:http://x/demo/你的jsp和你的servlet映射若是ROOT:http://x/你的jsp和你的servlet映射
uj5u.com熱心網友回復:
至于jsp怎么寫這個地址:最安全也最省心的是用c:url,
<c:url var="post_url" value="https://bbs.csdn.net/user/add" />
<form method="post" action="${post_url}">
</forum>
不論是demo還是ROOT, 你只需要寫的映射地址即可
uj5u.com熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/38928.html
標籤:其他
