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之间相互跳转了,记得路径要写对。
参考文献