[英]Java web.xml page routing
So here is my problem, I am somewhat new to programming, and now I want to make "web.xml" in Java EE project, to rout every url that contains "/profile/" to a profile page, where depending on the id after "/profile/" it'll show current users data. 所以这是我的问题,我对编程有些陌生,现在我想在Java EE项目中创建“ web.xml”,将包含“ / profile /”的每个URL路由到配置文件页面,具体取决于id在“ / profile /”之后,它将显示当前用户的数据。 I have the servlet mapped as below... 我将servlet映射如下:
<servlet-mapping>
<servlet-name>RoutServlet</servlet-name>
<url-pattern>profile/*</url-pattern>
</servlet-mapping>
But what I am supposed to do in the servlet so I can get the id, and show current users data? 但是我应该在servlet中做什么,以便获取ID并显示当前用户数据? Please give me an advice, or nice resource where I can see how it's done. 请给我一个建议或不错的资源,让我看看它是如何完成的。
Thank you in advance ! 先感谢您 ! :) :)
That looks fine for the web.xml
. 对于web.xml
看起来不错。 In short, if you're using a JAX-RS implementation (eg Jersey ), you should use the @Path
annotation with @PathParam
for the parameters, so something like: 简而言之,如果您使用的是JAX-RS实现(例如Jersey ),则应将@Path
批注与@PathParam
用作参数,因此类似:
@Path("/profile")
public class ProfileService {
@GET
@Path("/{id}")
public Profile getProfile(@PathParam("id") String id) {
//...
}
See the Oracle guide on RESTful Web services for some more background. 有关更多背景知识,请参见有关RESTful Web服务的Oracle指南 。
Firstly I would change tha mapping to the following: 首先,我将映射更改为以下内容:
<servlet-mapping>
<servlet-name>RoutServlet</servlet-name>
<url-pattern>/profile</url-pattern>
</servlet-mapping>
Secondly in RoutServlet
you should have a the following code to get the userId
其次,在RoutServlet
您应该具有以下代码来获取userId
int userId = Integer.parseInt(req.getParameter("userId"));
And finally any URL's that sends a request to RoutServlet
should look like this 最后,任何向RoutServlet
发送请求的RoutServlet
都应如下所示
http://someurl.com/profile?userId=//someId
This way you can let RoutServlet
retrieve all the information of the User depending on what Id is in the GET request. 这样,您可以让RoutServlet
根据GET请求中的ID检索User的所有信息。
I would let RoutServlet
put all the data it retrieves in an ArrayList<Object> profileData
, redirect to a JSP and retrieve the data there using Expression Language, but I dont know what you use/prefer to display the data. 我会让RoutServlet
将它检索到的所有数据放在ArrayList<Object> profileData
,重定向到JSP并使用表达式语言在那儿检索数据,但是我不知道您使用/更喜欢显示什么数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.