Jersey实现JSP页面转发和重定向

来源:互联网 发布:rpm qa grep mysql 编辑:程序博客网 时间:2024/04/29 00:26

有关REST和Jersey的介绍和使用参考:http://www.ibm.com/developerworks/cn/web/wa-aj-tomcat/

下面举例说明Jersey结合 Servlet实现页面重定向和转发的代码。

(1)页面转发

Service类(相当于Struts的Action)方法:

@POST@Path("add")@Consumes(MediaType.APPLICATION_FORM_URLENCODED)// @FormParam表示通过HTML表单传入参数public Viewable addStudent2(@FormParam("name") String name,@FormParam("dept") String dept,@Context HttpServletRequest request,@Context HttpServletResponse response) {// 保存name和dept的逻辑(略)request.setAttribute("resultString", "success");request.setAttribute("studentList", studentList);HttpSession session = request.getSession();session.setAttribute("user", "guohengj");// 跳转到/strudent.jspreturn new Viewable("/student.jsp", null);}

 Jsp页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>Result:${resultString }<br/>User: ${user }<form action="/jersey/rest/students/add" method="post"><inputtype="text" id="name" name="name" /><br /><input type="text" id="dept" name="dept" /><br /><input type="submit" /></form></body></html>

2)页面重定向

Service类方法:

@GET@Path("/admin")public void index (@Context HttpServletResponse response){try {response.sendRedirect("/admin/index.jsp");} catch (IOException e) {}}


参考地址:

1) http://stackoverflow.com/questions/6984338/how-to-forward-from-a-jax-ws-service-to-jsp

2http://topic.csdn.net/u/20110103/11/28b661ed-34ca-47a0-b37e-00d61407cadf.html?seed=972754557&r=78678808#r_78678808