| Author |
passing parameters from JSP to a Servlet
|
Rekha Pande
Ranch Hand
Joined: Jan 29, 2004
Posts: 145
|
|
Hi all... I have these dynamic string that are generated from a while loop String values = ""; Vector v1 = new Vector(); while(some.next()) { values = some.getString("values"); v1.addElement(values); } session.setAttribute("v1",v1.toString()); Now V1 has the values [1,23,45,34,56,78] now the group of values that are there in the vector i want to send it to a servlet.... how would i do that....The form method of my JSP is post ... Sample code will be highly appreciated Thanxs in advance PS: Intially when i tried to send just one string value from JSP to servelt it gave me this error: --------------- type Status report message HTTP method POST is not supported by this URL description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL). ------------------ So i changed the JSP form method to get... then the JSP page was succesful in calling the servlet but was not able to retreive value by using session.getAttribute("v1");... plz tell me how would i pass value from JSP to a servlet
|
 |
pinky yadav
Ranch Hand
Joined: Jun 17, 2002
Posts: 44
|
|
there are many ways to pass a value from jsp to servlet. 1) put value in session and retrieve it in servlet. on jsp page session.setAttribute("v1",v1). this puts the v1 in session which you can retrieve in servlet by req.getSession().getAttribute("v1") 2) pass the value in url string for get method. you need to do req.getParameter("v1") in servlet. hope it helps pinky
|
 |
Rekha Pande
Ranch Hand
Joined: Jan 29, 2004
Posts: 145
|
|
No it didn't work with the seesion attributes.. plz tell me where am i going wrong.. my code is below THIS IS THE JSP FILE CODE ------------------------- <html> <head> <title> TEST </title> </head> <body> <% String sample = ""; if(request.getParameter("sample")!=null) { sample = request.getParameter("sample"); } session.setAttribute("sample",sample); %> <h1> JBuilder Generated JSP </h1> <form name = "encryption" method="GET" action = "http://localhost:8080/servlet1"> <br>Enter password : <input name="sample" size="20"> <input type="submit" name="Submit" value="Submit"> <input type="reset" value="Reset"> <br> </p> </form> </body> </html> THE SERVLET CODE IS AS BELOW: ------------------------------ package test_date; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class Servlet1 extends HttpServlet { static final private String CONTENT_TYPE = "text/html"; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Get request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String sample = ""; response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html>"); sample = request.getSession().getAttribute("sample").toString(); out.println("The sample value is " + sample); out.println("<head><title>Servlet1</title></head>"); out.println("<body>"); out.println("<p>The servlet has received a GET." + sample+ "This is the reply.</p>"); out.println("</body></html>"); } //Clean up resources public void destroy() { } } where am i going wrong..... y isnt the sample string passed on from jsp tp the servlet..!!???
|
 |
Rekha Pande
Ranch Hand
Joined: Jan 29, 2004
Posts: 145
|
|
and i don't wish to pass parameter with url as i have to pass around 50 to 60 values which are generated from the while loop... thanxs..
|
 |
Rekha Pande
Ranch Hand
Joined: Jan 29, 2004
Posts: 145
|
|
and y is it that when in the jsp my form tag has the method = "POST" it gives this error while calling the servlet....??? ----------------- type Status report message HTTP method POST is not supported by this URL description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL). ------------------
|
 |
Rekha Pande
Ranch Hand
Joined: Jan 29, 2004
Posts: 145
|
|
and i don't wish to pass parameter with url as i have to pass around 50 to 60 values which are generated from the while loop... thanxs..
|
 |
M. Gagnon
Ranch Hand
Joined: Feb 18, 2004
Posts: 68
|
|
|
In the code you pasted here, it looks as though you are trying to get the form field with request.getParameter() on the same page as the form, before it has been submitted. (Looking at what you pasted under "THIS IS THE JSP FILE CODE") You should be able to use request.getParameter("sample") in the servlet. Can't think of why using method="POST" in the form wouldn't work, but if you are going to use method="GET", then you must put your code in the doGet() method or have doGet() call doPost() passing the same request and response objects that doGet() received.
|
 |
 |
|
|
subject: passing parameters from JSP to a Servlet
|
|
|