如何将参数传递给servlet [英] How to pass parameter to servlet

查看:170
本文介绍了如何将参数传递给servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < input name =deleteGameButtontype =submitvalue =Delete
onclick =submitToServlet('DeleteGameServlet');>

以下是相应的javascript:

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

我希望servlet能够访问userBean

 < jsp:useBean id =userBeanscope =sessionclass =org.project.User/> 


解决方案



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



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



在Servlet调用请求.getParameter(name);



PS自动执行此类操作 USE STRUTS 。 :-) Struts完全按照你想要的:在将参数传递给action之前,它用这些参数填充bean。透明。

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:

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

Here is the corresponding javascript:

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

I'd like the servlet to have access to userBean

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

解决方案

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.

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

In Servlet call 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.

这篇关于如何将参数传递给servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆