页面从http://localhost:8080自动跳转到指定页面(之前的Web日历)

本文介绍如何在JSP页面中使用JavaScript调用Servlet的`/main`路径,包括直接和延迟跳转,并展示了成功与错误页面示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意不要把地址写成main.jsp了,这时还没有携带数据呢,写sevlet中的main
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: U100926
  Date: 2021/12/29
  Time: 17:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>

<!-- 运行javaScript不用额外安装包,自带了 -->
<script>window.location.href='main';</script>

<!--方法1-->
<!--2种写法都可以,单双引号都可以,注意不要写成了main.jsp会报空指针异常,此时jsp页面还没有携带参数!要写servlet在地址栏的映射main,如下
@WebServlet("/main")
public class Main extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {.......}
<script>window.location.href='main';</script>
<script>window.location.href="main";</script>
<script>window.location.href='http://localhost:8080/main';</script>
<script>window.location.href='success.jsp';</script>
 -->

<!--方法2-->
<!--
<script>setTimeout("location.href='main'", 0);</script>
<script>setTimeout("location.href='error.jsp'", 0);</script>
-->

</body>
</html>

测试用的2个页面

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: U100926
  Date: 2021/12/30
  Time: 10:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>success</title>
</head>
<body>
jump success
</body>
</html>

error.jsp

<%--
  Created by IntelliJ IDEA.
  User: U100926
  Date: 2021/12/30
  Time: 10:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>error</title>
</head>
<body>
jump error
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值