实现JSP和Servlet相互跳转

JSP和Servlet相互跳转

在之前写项目的时候遇到了JSP跳转Servlet的问题,一开始网上查教程的时候Servlet里也有HTML内容,以为可以直接跳,结果失败了,后续发现是需要讲Servlet对应的JAVA文件写入到web.xml之中去。

具体情况

当时是已经写好Servlet文件并且写好跳转到JSP,因为需要在到达之前将后端的数据传输给JSP将其打印为表格的形式。

@WebServlet(name = "ResultServlet", value = "/res")
public class ResultServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String event=req.getParameter("event");
        req.setAttribute("event",event);
        String eventsCommand[]={"result women 1m springboard detail" ,
                "result women 3m springboard detail" ,
                "result women 10m platform detail" ,
                "result women 3m synchronised detail" ,
                "result women 10m synchronised detail" ,
                "result men 1m springboard detail" ,
                "result men 3m springboard detail" ,
                "result men 10m platform detail" ,
                "result men 3m synchronised detail" ,
                "result men 10m synchronised detail"};
        String events[]={
                "women 1m springboard" ,
                "women 3m springboard" ,
                "women 10m platform" ,
                "women 3m synchronised" ,
                "women 10m synchronised" ,
                "men 1m springboard" ,
                "men 3m springboard" ,
                "men 10m platform" ,
                "men 3m synchronised" ,
                "men 10m synchronised"
        };
        DataSourceImpl data = new DataSourceImpl();
        for (int i = 0; i < eventsCommand.length; i++) {
            List<HeatClass> list=data.getEventDetail(eventsCommand[i]);
            req.setAttribute(events[i],list);
        }
        req.getRequestDispatcher("result.jsp").forward(req,resp);

    }

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


}


操作步骤

在tomcat里的WEB-INF文件夹,打开web.xml,往其中写入Servlet对应的映射和路径。

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>Athlete</servlet-name>
        <servlet-class>web.AthleteServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>Result</servlet-name>
        <servlet-class>web.ResultServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>Schedule</servlet-name>
        <servlet-class>web.ScheduleServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Athlete</servlet-name>
        <url-pattern>/Athlete</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Result</servlet-name>
        <url-pattern>/Result</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Schedule</servlet-name>
        <url-pattern>/Schedule</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>


</web-app>

之后跳转时需要用form来传输参数给Servlet

<div class="action">
    <table>
        <tr>
            <input class="btn" type="button" onclick="window.location.href='index.jsp';" value="OVERVIEW">
        </tr>
        <tr>
            <input class="btn" type="button" onclick="window.location.href='schedule.jsp';" value="SCHEDULE">
        </tr>
        <tr>
            <input class="btn" type="button" onclick="window.location.href='';" value="THE MOMENT">
        </tr>
        <tr>
            <form action="Result">
                <input class="btn" type="submit" value="RESULTS">
            </form>
        </tr>
        <tr>
            <form action="Athlete">
                <input class="btn" type="submit" value="ATHLETES">
            </form>
        </tr>
    </table>
</div>

这样子就能实现jsp和servlet之间相互跳转了,记得路径要写对。
参考文献

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值