Jersey Rest webservice redirecting to the same page(Jersey Rest web 服务重定向到同一页面)
问题描述
I have a simple rest webservice that will be used to load a page. Once the page is loaded the same page will be displayed with a confirmation msg or error msg being displayed.
Im using the using the below code to load it ....
jsp page:-
<form action="rest/file/upload" method="post"
enctype="multipart/form-data">
<br> <label>Username:    </label><input type="text"
placeholder="Username" name="username"> <br> <br>
<label>Password:     </label><input type="text"
placeholder="Password" name="password"> <br> <br>
<hr>
<p>
Select a file : <input type="file" name="file" size="45" />
</p>
<br> <input id="submit" type="submit" value="Upload" />
<c:out value="${obj}" />
<!-- ${obj} -->
</form>
controller
@Path("/file")
public class FileUploadService {
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Viewable uploadFile(
@Context HttpServletRequest request,@Context HttpServletResponse response,
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail,
@FormDataParam("username") String username,
@FormDataParam("password") String password) throws NoSuchAlgorithmException, IOException, URISyntaxException {
response.setHeader("Location", "/");
return new Viewable("/index.jsp",null);
web.xml
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Once I click the form the file is loaded and it takes back to the index.jsp page, but the location of the page is set to. RESTFileUpload is the program name.
http://localhost:8080/RESTFileUpload/rest/
but I want it to be
http://localhost:8080/RESTFileUpload/
I don't know much (or really anything) about the MVC features Jersey, but another option is to just use a redirect. You can simply use Response.seeOther(URI)
. This will send out a "303 See Other" with the Location
header. The browser should send another request for this page. The method might look something like
public Response getRedirect(@Context ServletContext context) {
UriBuilder builder = UriBuilder.fromPath(context.getContextPath());
builder.path("index.jsp");
return Response.seeOther(builder.build()).build();
}
This will redirect to /contextPath/index.jsp
(or in other words the index.jsp
path located in the webapp root)
As an aside, if you are familiar with Javascript/jQuery at all, there are other file upload options that don't involve the redirect.
UPDATE
Just to show that this works fine
@Path("/redirect")
public class RedirectResource {
@GET
public Response getRedirect(@Context ServletContext context) {
UriBuilder builder = UriBuilder.fromPath(context.getContextPath());
builder.path("index.html");
return Response.seeOther(builder.build()).build();
}
}
这篇关于Jersey Rest web 服务重定向到同一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jersey Rest web 服务重定向到同一页面
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01