简体   繁体   English

如何在servlet中创建URL?

[英]How to create a URL in a servlet?

i want to know how to generate a url in a servlet. 我想知道如何在servlet中生成一个url。 I have a login servlet, and every time that add a user i want to gen. 我有一个登录servlet,每次添加一个我想要的用户。 a url for each user profile. 每个用户个人资料的网址。 Can anyone help me please? 有人可以帮我吗?

The easiest way is to declare a servlet mapping like the following: 最简单的方法是声明如下的servlet映射:

  <servlet-mapping>
    <servlet-name>UsersSelvlet</servlet-name>
    <url-pattern>/Users/*</url-pattern>
  </servlet-mapping>

Now, whenever you get a request for MyApp/Users/UserId you read the request path, get the userId and check if the user exists. 现在,每当您收到MyApp / Users / UserId请求时,您都会读取请求路径,获取userId并检查用户是否存在。 If not you return 'Not found'. 如果不是,则返回“未找到”。 Otherwise you return the user's page. 否则,您将返回用户的页面。

This is a quick and dirty implementation of a RESTful service. 这是RESTful服务的快速而又脏的实现。

I think the solution of kgiannakakis is very good. 我认为kgiannakakis的解决方案非常好。 I just want to add some details, because reading the comment of Agusti-N I have the suspect that may be he is missing something. 我只想补充一些细节,因为阅读Agusti-N的评论我怀疑可能是他遗漏了一些东西。

Let's say that you have the UsersServlet described by kgiannakakis, a jsp called showUserProfile.jsp and an userBean that has all the properties of the user's profile needed to be shown in the jsp. 假设您有kgiannakakis描述的UsersServlet,一个名为showUserProfile.jsp的jsp和一个userBean ,它具有需要在jsp中显示的用户配置文件的所有属性。

When a new user registers to your application, you need to do nothing more than you already do now. 当新用户注册到您的应用程序时,您只需执行比现在更多的操作。 Just register a new user in the db, and forget the login servlet . 只需在db中注册一个新用户, 忘记登录servlet

Now suppose that I registered to your app with my username alexmeia . 现在假设我使用我的用户名alexmeia注册到您的应用程序。

When someone digit the url yourApp/Users/alexmeia the UsersServlet is called . 当有人对url yourApp / Users / alexmeia的数字进行数字调用时会调用UsersServlet This servlet gets the username alexmeia from the request url, checks in the DB if this username exists and if exist load all the properties of this user in the userBean . 此servlet从请求URL获取用户名alexmeia,检查数据库中是否存在此用户名,如果存在,则在userBean中加载此用户的所有属性

After that, forward to showUserProfile.jsp , which shows the user profile reading it from the userBean. 之后,转发到showUserProfile.jsp ,它显示从userBean读取它的用户配置文件。

Obviously, if the user alexmeia is not in the Db, you can redirect to a generic userNotFound.jsp, or go to home page and show a message and so on... 显然,如果用户alexmeia不在Db中,您可以重定向到通用userNotFound.jsp,或者转到主页并显示消息等等......

This works for all the registered users in the same way. 这适用于所有注册用户的方式相同。 You don't need to really create a real new url for every new user. 您不需要为每个新用户真正创建一个真正的新URL。

It sounds like you might want to look into REST technologies. 听起来您可能想要研究REST技术。 There is a tutorial here you might want to have a look at. 这里有一个教程 ,你可能想看看。

Do you need URL rewriting? 你需要URL重写吗? Something like this , perhaps, but instead of RMI generate your own user id 或许这样的事情,但不是RMI生成自己的用户ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM