java 如何将参数传递给servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/234090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:32:58  来源:igfitidea点击:

How to pass parameter to servlet

javahtmlformsjspparameters

提问by pypmannetjies

How do I pass a parameter from a page's useBean in JSP to a servlet in Java? I have some data in a form that gets passed no problem with a submit button, but no way to send anything else. Please help? Here is my code:

如何将参数从 JSP 中页面的 useBean 传递到 Java 中的 servlet?我在表单中有一些数据,通过提交按钮没有问题,但无法发送任何其他内容。请帮忙?这是我的代码:

<input name = "deleteGameButton" type = "submit" value = "Delete"
 onclick = "submitToServlet('DeleteGameServlet');">

Here is the corresponding javascript:

这是相应的javascript:

 function submitToServlet(newAction)
 {
   document.userGameForm.action = newAction;
 }

I'd like the servlet to have access to userBean

我希望 servlet 能够访问 userBean

 <jsp:useBean id = "userBean" scope = "session" class = "org.project.User" />

回答by Vladimir Dyuzhev

You kind of mess things here.

你把事情搞得一团糟。

onclick() is Javascript and executed on client side. It has no (direct) way to update session-scoped bean. That bean is left on server-side, and was used when the HTML page was generated. To pass parameters back to servlet you need to use good old form fields, and submit the form.

onclick() 是 Javascript 并在客户端执行。它没有(直接)方法来更新会话范围的 bean。该 bean 留在服务器端,并在生成HTML 页面时使用。要将参数传递回 servlet,您需要使用良好的旧表单字段,然后提交表单。

Add more fields to the form, set their values before submit, then submit.

在表单中添加更多字段,在提交之前设置它们的值,然后提交。

In Servlet call request.getParameter("name");

在 Servlet 中调用 request.getParameter("name");

P.S. To automate this kind of things USE STRUTS. :-) Struts does exactly what you want: before passing the parameters to action, it populates the bean with those parameters. Transparently.

PS 要自动化此类事情,请使用 STRUTS。:-) Struts 完全符合您的要求:在将参数传递给操作之前,它使用这些参数填充 bean。透明地。

回答by mtruesdell

It depends exactly what you are trying to do. The

这完全取决于您要尝试做什么。这

<jsp:useBean id = "userBean" scope = "session" class = "org.project.User" />

<jsp:useBean id = "userBean" scope = "session" class = "org.project.User" />

tag will allow you to use the userBean attribute of the session in your jsp. If there is not a userBean attribute in the session, it will create a new one (using the default constructor for org.project.User) and place it in the session.

标记将允许您在 jsp 中使用会话的 userBean 属性。如果会话中没有 userBean 属性,它将创建一个新属性(使用 org.project.User 的默认构造函数)并将其放置在会话中。

Then, when you get to the servlet, you can retrieve it with:

然后,当您到达 servlet 时,您可以使用以下命令检索它:

User user = (User)request.getSession().getAttribute("userBean");

回答by Naveen kumar HR

 getServletConfig().getServletContext().getRequestDispatcher("servlet path & name");         
 dispatcher.forward (request, response);

回答by David Santamaria

Hi try with the next tag:

嗨,尝试使用下一个标签:

<jsp:useBean id = "userBean" scope = "session" class = "org.project.User"/>
 <jsp:setProperty name="beanName" property="propertyname" value="value"/>
</jsp:useBean>

more here

更多在这里

Good luck!

祝你好运!