Accessing Post Variables Using Java Servlets

Accessing post variables using Java Servlets

Your HttpServletRequest object has a getParameter(String paramName) method that can be used to get parameter values. http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)

Java get parameters from post request

Use a servlet container like tomcat.

Then, study Servlets (extends HttpServlet), you can use its GET and POST method.

Here is the POST.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
username = request.getParameter("username");

}

//username is a parameter that was sent via POST from a JSP.

Servlet get GET and POST's parameters at the doPost method

The getParameter() method can return (if possible) both GET and POST parameters as it works transparently between GET and POST. You don't need to do any explicit work to get the GET parameters. you can use getParameter for both query parameters and POST parameters.

But should you do it? - It's considered a poor design practice especially if there is sensitive information to be sent.

Take a look at this answer:

  • HTTP POST with URL query parameters -- good idea or not?

Java servlet can't get POST params

When you're submitting the form via ajax are you sending the data in JSON format?
if you are then, you should explicitly fetch the data in your jquery or js file from the userId and password box and then send it to the server in JSON format. That would fix the problem.

POST from current JSP to Servlet values from a previous JSP

you can put them in hidden fields like this in the form:

<form name="calcsum" action="PlusServlet" method="POST">
<% String Number1 = request.getParameter("Number1"); %>
<% String Number2 = request.getParameter("Number2"); %>
<br>
<span>Number1= <%= Number1 %> from calc.jsp form. </span>
<br>
<span>Number2= <%= Number2 %> from calc.jsp form. </span>
<br><br><br><br><br>
<input type="hidden" value="<%= Number2 %>" name="Number1" />
<input type="submit" value="Plus-Calc" name="Plus-Calc" />
</form>

How to post a data from a servlet in a variable to a servlet?

You can forward (server side) the request to the next servlet:

RequestDispatcher dispatcher = request.getRequestDispatcher("/nexturl");
dispatcher.forward(aRequest, aResponse);

You can attach the decoded variable to your session object and retrieve it from there in the servlet you forward to. (Or, if the servlet can be called with a parameter too, check the session for the variable (remove it when you use it) and if it's not there try to parse the apropriate parameter.)

Update

To use the HTTP session as a way to pass your variable, add it:

HttpSession session = request.getSession();
session.setAttribute("name", "value");

and retrieve it in the next servlet:

HttpSession session = request.getSession();
String value session.getAttribute("name");
session.removeAttribute("name");

The session is created automatically by the servlet container, if uses a session cookie to map session state to a series of HTTP requests from the same browser session.

Java Servlets: Differentiate between GET and POST

The query string is trivial to parse, thus gives you the URI query param names, while the getParameterNames() gives you the whole set.

Split the query string by '&', then subsplit each token by '='.
For each key and value, perform the URLDecoder.decode(). That's all.

Toss all such keys in a set. If the param is in the uri query set it a good chance it's only there. If you must find if it is also in the post, actually, post form-encoded data is also coded like that, but that post is consumed so it's too late. Besides, the post could also be a multipart encoding which is non-trivial decode.

In the end, it's odd that you need this distinction. Can you explain for what purpose you seek this distinction?



Related Topics



Leave a reply



Submit