How to pass parameter to servlet

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" />

Asked by: Sienna792 | Posted: 23-01-2022

Answer 1

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.

Answered by: Lydia400 | Posted: 24-02-2022

Answer 2

It depends exactly what you are trying to do. The

<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.

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

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

Answered by: Melissa899 | Posted: 24-02-2022

Answer 3

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

Answered by: Dexter454 | Posted: 24-02-2022

Answer 4

Hi try with the next tag:

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

more here

Good luck!

Answered by: Daisy490 | Posted: 24-02-2022

Similar questions

java - how can i pass a object parameter from a servlet to a jsp page?

the object i want to pass is a list of item, item class described below : class item{ String val1 String val2 String val3 } //with getter setters How can i pass items ( List items = new ArrayList() ) from a servlet to a jsp page and what do i have to add in the jsp to access the object ?

Java servlet upload file and get post parameter

I want to upload from iphone to servlet registration data and pictures of user. Can I do that in one request or should I do two requests? Thanks a lot :)

java - Can't get parameter from the page via servlet

It should be simple, but i have a problem, This is my *.jsp file &lt;html&gt; &lt;head&gt; &lt;title&gt;Edit DataBase data&lt;/title&gt; &lt;meta http-equiv="Content-Language" content="en-us"&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=windows-1252"&gt; &lt;meta name="GENERATOR" content="Microsoft FrontPage 4.0"&gt; &lt;meta name="ProgId" content="FrontPage.Editor.Document"&gt; &lt;ti...

javascript - Getting Ajax parameter from inside Java servlet

my data: var test = {cars : []}; var cars = [] cars.push({ "name" : "Ford", "year" : "2000" }); cars.push({ "name" : "Audi", "year" : "2002" }); = cars; var json = JSON.stringify(test); $.get('/myservlet/', json, function(data) { // also tried .getJSON , .post alert('Success'); }) In Java I get the "json" vari...

java - put a parameter in the servlet url

I have this peace of code: &lt;% ArrayList&lt;Utente&gt; lista=null; try{ lista= (ArrayList&lt;Utente&gt;)request.getAttribute("lista"); }catch(Exception e){ e.printStackTrace(); } if(lista!=null){ for(int i=0;i&lt;lista.size();i++){ out.print("&lt;tr&gt;"); out.prin...

java - servlet parameter ORM

Have any framework like JPA make servlet request parameter auto mapping to object? ex : class Object1 { String name; int count; Set&lt;Object2&gt; oset; } class Object2 { String name; int num; } Object1 o = Xframework.doBind(request, Object1.class);

java - Use servlet filter to remove a form parameter from posted data

A vendor has been posting XML data over HTTPS within a form variable named XMLContent to my Coldfusion application server. I recently moved to a more recent version of the application server and those requests are throwing 500 server errors. It is throwing the error because a second form parameter's content is not properly urlencoded, but I don't need that parameter anyway. (I contacted the vendor to fix this but they are ...

Get parameter sent via jquery ajax in Java Servlet

This question already has answers here:

java - How to send parameter from JSP to servlet using Ajax call?

I need to send a parameter to a servlet through an ajax call i am trying with this code but it is not working.I am posting my code and the way i catch it to the Servlet... function loadJson() { $.ajax({ url: "JsonServlet", type: "post", dataType: "json", data: { "value": "2" }, error: function () { alert("error occured!!!"); }, success: function (dat...

java - Servlet get Parameter and Null Value for Hidden form field

I have form field something like below &lt;form action="test" name="formtest" method="post"&gt; &lt;input type="hidden" name="testid" value ="12345"&gt; &lt;/form&gt; Now in servlet I do something like int testid = Integer.parseInt(request.getParameter("testid")); I get Null pointer exception and somehow I believe even those form has value , its not getti...

java - How to format methods with large parameter lists

I have never seen a way to do this nicely, i would be interested in seeing how others do it. Currently i format it like this: public Booking createVehicleBooking(Long officeId, Long start, Long end, String origin, String destination, ...

java - Optional Parameter in MVEL Functions

Is there a way to get MVEL 2.0 ( ) to work with functions with optional parameters? I would like to be able to eval this: trunc('blahblah',2) but also trunc('blahblah',2,'[...]'); Now i have tried: def trunc(param1,param2,param3) { ... impl ... } That gives an exception if i t...

java - Why null cast a parameter?

This question already has answers here:

generics - Is there a way to pass a Number as a parameter in Java?

I'm allocating an array of T, T extends Number inside a class. I can do it this way: myClass test = new myClass(Double.class, 20); Then the constructor itself: myClass(Class&lt;T&gt; type, size) { array = (T[]) Array.newInstance(type, size); } I'd like to know if it's possible to do it like this: myClass(Number n, size) { array = (T...

java - How do I encode URI parameter values?

I want to send a URI as the value of a query/matrix parameter. Before I can append it to an existing URI, I need to encode it according to RFC 2396. For example, given the input: &amp; value2 I expect the output: Neither nor

java - Detect changing value of object passed as parameter

I'm now working with code that looks like this public String getName(User user) { user.setSth(...); return user.getName(); } I think it's bad practice to change objects passed as parameters. Is there a tool that detects that kind of code? I looked at findbugs, pmd and checkstyle, but could not find any check for this. P.S. sorry for bad example.

java - What name do you use for the parameter in a static variable setter method?

When I write setters for instance methods, I use this to disambiguate between the instance variable and the parameter: public void setValue(int value) { this.value = value; } So, what do I do when value is a class variable (static) instead of a member of an instance? private static int value = 7; public static void setValue(int value) { value = value; // compile fails;...

generics - Is it possible to remove the "Class<T>" parameter from this Java method?

I have a Java method with the following signature: public &lt;T&gt; List&lt;HtmlOptionsComposite&gt; convertToHtmlOptionsCompositeList (List&lt;? extends T&gt; objects, Class&lt;T&gt; clazz, String getValueMethodName, String getDescriptionMethodName, String getDiscontinuedMethodName) { ... } The clazz parameter is required by the method to use refle...

Calling a Java method with the implicit Graphics parameter, like repaint() or accessing the Graphics in a JPanel

When you call repaint(), for example, repaintComponent(Graphics) gets called, and then you can call it from outside the class without the parameter Graphics. I'd like to have a function that takes more parameters I'm using, but I still want to be able to draw with it, so I need to be able to make a call like that (eg repaint() calls repaintComponent(Graphics)) or get access to Graphics to call the function, assumin...

How do I pass a Java object as a parameter to a MATLAB function?

I wrote a Matlab class to implement a database using JDBC and stuff from java.sql. I need to know how many results were in a ResultSet, so I wrote the following Matlab static function: methods (Static) function [numRecords] = numRecords(resultSet) numRecords = 0; if (~isempty(resultSet)) row = resultSet.getRow(); resultSet.beforeFirst(); resul...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)