I have a client-side GWT-FormPanel, which has an import file functionality. I want to pass cookie along with the form to the action on the server-side.
I tried this method: Passing parameters along with a multipart/form-data upload form (Java Http Post Upload)
I tried adding a hidden variable as well. But it does not seem to work with ENCODING_MULTIPART.
However I am concerned I have to pass multiple variables, is this method to go about the problem?
final FormPanel form = new FormPanel();
form.setAction("./abc/sheets/importReport");
form.setMethod(FormPanel.METHOD_POST);
form.setEncoding(FormPanel.ENCODING_MULTIPART);
Is there any other method to pass parameters from the client to the server in the Http request?
Can anyone help me out?
Thanks in Advance.
Related
I'm start learning java programming, and I want make a simple server application. I read about com.sun.net.httpserver.HttpServer and find a good example on this link: https://github.com/imetaxas/score-board-httpserver-corejava.
I understand how to do Get-request in url, but I don't know how POST works. I think it must be sent a form or data on the server.
I attach the link of project, which I'm learning, in readme the author wrote http://localhost:8081/2/score?sessionkey=UICSNDK - it's not working...
I wrote in url and get sessionkey: "localhost:8081/4711/login --> UICSNDK"
I wrote in url this for Post request: "localhost:8081/2/score?sessionkey=UICSNDK" - not working and in chrome return 404 bad request
3.wrote in url this:"localhost:8081/2/highscorelist"
Please help me, I am beginner.
The difference between GET and POST is that with a GET request the data you wish to pass to the endpoint is done by modifying the url itself by adding parameters to it.
With a POST any data you wish to send to the endpoint must be in the body of the request.
The body of a request is arbitrary data that comes after a blank line in the header The reqiest has the request line, following by any number of header attributes, then a blank line.
The server would need to know what the format of the body of the request was and parse it as appropriate.
Of course 'modern' frameworks like jax-rs allow you to automatically convert request data to objects, so that it is much simpler.
How can I pass the value of a variable from javascript to a java variable?
<% String st = "<script>document.writeln(selected)</script>";
out.print("value = " + st);%>
This is my code for java getting the values from javascript variable, selected. But no value is displayed.
You have to make a request and send your variable from the browser (where Javascript lives) to the server (where the JSP lives) that way.
Ajax would work, or an HTML form.
In you JSP, you can then receive it as a request parameter (or as part of the request body, or as a request header).
Javascript runs on client. JSP runs on server. The only way to pass information from client to server in web environment is via HTTP parameters or HTTP headers during HTTP request or as a part of request body if method is POST or PUT.
So, you should create such request. It can be done using either changing of your document location or utilizing AJAX call.
you can pass parameter or make a hidden field inside your jsp code and using javascript assign value for this hidden field, then get parameter value in java code.
Use HTML forms.
On server side, you'll get the data in HTTPServletRequest parameter.
Check this too: Building my first Java Web Application
I am able to get data from doPost() in java servlet and process the data accordingly. After that, i need to send a redirection response which I am not clear about. I intend to send the redirection page to an external php site.Anyone would share with me how could this be achieved? I have tried response.sendRedirection(URL); in doPost(), but it is not working and not redirect me to any new page.
Thanks for reading.
The method is response.sendRedirect() not response.sendRedirection(). That could be why you're getting the error.
You wrote response.sendRedirection(URL). The method is called response.sendRedirect(URL).
Or do you write something in the output before invoking the response.sendRedirect()? Im not sure how jsp handles this but, for example, in php you only can change the header before any actual output is sent.
use response.sendRedirect(url) instead of response.sendRedirection(url)
I hava a servlet which handles some resources files, and I need to add a response header before I forward the request to the real jsp file.
response.setHeader("a", "b");
request.getRequestDispatcher("1.jsp").forward(request, response);
I need to send that header directly to the browser, But it did not work, I tried to use firebug to watch the http request and its response, how can I do that?
Try to use .include(request, response) instead. Probably it's a .forward() feature to fully clean response object before forwarding.
See http://download.oracle.com/javaee/5/api/javax/servlet/RequestDispatcher.html
How do you know that it is not working? Please read this JR thread, I believe you are expecting similar thing.
If you want to use some data added by the servlet in the 1.jsp code, I suggest you use request.setAttribute method. response.addHeader/setHeader put some data into the response'header. Generally the data in the response header is used by the browser.
The headers are being cleaned up. Just curious, what stops you from using request.setAttribute()?
This question already has answers here:
How to use java.net.URLConnection to fire and handle HTTP requests
(12 answers)
Closed 7 years ago.
If I use a browser to send information to the server (for example using a log-in, password page), I just fill the user text-box and the password text-box and clicking on the log-in button.
I would like to send this information but without having to use the browser. I would like to 'fill' the text-boxes but without having to do it manually in the browser. May be using a Servlet.
My question is: How to send information in text-boxes, for example, to a website, doing it from a Servlet?
why not just make a call to the URL from Java using a URL like http://your.domain.name/your/servlet/path?userFieldName=THE_VALUE_YOU_WANT_TO_PASS&passwdFieldName=PASSWORD
The servlet will feel like the values are coming from those boxes.
Or you may want to dive into Apache HTTP Client to mimick a request sent from an client.
uh..oh.. are you doing functional testing? Why not look into JMeter?
Updates as per comment
You need to know what actually form submission does? It basically forms a query string composed of Key-Values (KV) pair.
So, if you have a a text field named tfield where user has typed some text, and there is a drop down named, ddfield where user has selected optionX which has value optionX-Val. And this form gets submitted to a URL, http://my.domain.name/my/servlet -- the browser will send a request which will look like
http://my.domain.name/my/servlet?tfield=some%20text&ddfield=optionX-Val
If you want to mimic form submission, you will have to manually create a URL that has a request string containing all the fields and their values as FIELD_NAME=FIELDVALUE ordered pair separated by ampersand (&)
ah, great idea. If you use Firebug (a Firefox extension), open the NET panel in Firebug, make a manual submission of the form that you wanted to mimic. See what request is posted when you submitted the form. It will have exact URL format that you are after. Copy this URL, replace the values and make fake submissions as much as you want.
Hope this helps.
It is not clear to me what you really up to. I assume that the servlet will be the one who will send the data. Here some examples.
Using setAttribute then Forward the request
//On your servlet
request.setAttibute('user', 'admin');
request.setAttribute('password', '123');
getServletContext().getRequestDispatcher("page.jsp").forward(request, response);
//On your jsp page get the value using EL
<span>${user}</span>
Using session
//On your servlet
HttpSession session = request.getSession(true);
session.setAttribute('user', 'admin');
session.setAttribute('password', '123');
getServletContext().getRequestDispatcher("page.jsp").forward(request, response);
//On your jsp page get the value using EL
<span>${user}</span>
The above example is intended to work within the web application. To send information to another web application, which expecting a request. See sample below.
//On your jsp or servlet, you can also do the same within web application
request.sendRedirect('http://example.com?user=admin&password=123');
//on your jsp #example.com
<span>${param.user}</span>
If this is not what you mean, adding more details will be a help.
a servlet takes care of the other end: it's basically a handler for http requests that lives inside a servlet container. If I understand you correctly, you're wanting to send an http request. You can do that using command-line tools like curl, or if you want to stay within java land, you could try this example on exampledepot. Use your favourite search engine to search for more examples, e.g. with search terms such as "sending GET requests through a url".
In your situation, where you need to send information for username and password, you would need to look at the html and find the url for the form element's action attribute. Then you need to find the names of the username and password fields. Using these names as url parameters, you can construct a GET request that mimics sending a form.
NOTE: usually storing a password in plain text in code and/or sending it in plain text to a website is not a good thing to do.
Just in case anyone is interested, there is a plugin for Firefox called Tamper data. With it you can stop the sending of and http request and modify it. It will show you the "url" you need for sending the params, the values they currently have, and their name. You can check it out here. After that you can use a request.sendRedirect('url you got from Tamper Data');