I have a simple form in my Spring MVC project that handles a file upload.
form.jsp
<form method="POST" enctype="multipart/form-data" action="/upload" id="form">
<input id="file" type="file" name="file"><br />
<input type="submit" value="Upload">
<input type="reset" value="Reset">
</form>
Because of the form's action however, I am redirected to localhost:8080/myApp/upload rather than localhost:8080/myApp/form as specified in my #Controller
#RequestMapping(method = RequestMethod.POST, value = "${context}/upload", headers = "content-type=multipart/*")
public String uploadFile(#RequestParam("file") MultipartFile file, Model model) {
//do some upload stuff
model.addAttribute("uploadSuccess", "Upload Successful.");
return "/form";
}
Is it possible to prevent only the form action's redirection, but still retain the value ${context}/upload so that my Controller method gets called? I need my page to land back on myApp/form because this particualr url makes a call to a separate Controller method that retrieves some data
Note that I don't wish to rename my jsp file to 'upload.jsp' and I don't want to make an AJAX Post (Thanks for nothing IE9). Any thoughts mates?
Found the answer for my exact case on 17.5.3 Redirecting to views from Spring's Docs
It is sometimes desirable to issue an HTTP redirect back to the
client, before the view is rendered. This is desirable, for example,
when one controller has been called with POST data, and the response
is actually a delegation to another controller (for example on a
successful form submission). In this case, a normal internal forward
will mean that the other controller will also see the same POST data,
which is potentially problematic if it can confuse it with other
expected data.
This can be done with the following:
return "redirect:/form";
A nice benefit to the redirect is that it prevents the user from accidentally re-POSTing the same data by performing a refresh. The refresh forces a GET of the result page, not a resend of the initial POST data.
UPDATE:
The default redirect:/view will append your model attributes to your URL as query params. This may be undesirable in some cases as it could expose sensitive information to the user. Utilize RedirectAttributes addFlashAttribute(String attributeName, Object attributeValue) method to in order to store Flash Attributes that will be used to store attributes when redirected from one URL to another (Success/Failure messages, etc.)
I am developing a LinkedIn login hook following this example but I got stuck at passing parameters from my .jsp file to the .java class implementing AutoLogin.
If I write a portlet, the values are sent correctly to a processAction method, however here the same approach is not working.
In my linkedin.jsp file i have the following (simplified) structure.
<%
PortletURL linkedInRegiserURL = renderResponse.createActionURL();
linkedInRegiserURL.setParameter(ActionRequest.ACTION_NAME, "linkedInRegister");
%>
<form id="linkedInForm" action="<%= linkedInRegiserURL.toString() %>" method="post"
name='<portlet:namespace/>linkedInForm'>
<input type="hidden" name='<portlet:namespace/>email' id="email" />
</form>
And then inside a javascript method, based on the LinkedIn API, I populate my input and then submit the form.
document.getElementById('email').value = member.emailAddress;
document.getElementById('linkedInForm').submit();
Everything is fine here. The problems start inside the login() function in my LoginHook implements AutoLogin class. If I do a print test, the following results are shown:
#Override
public String[] login(HttpServletRequest request,
HttpServletResponse response) throws AutoLoginException {
String email1 = ParamUtil.getString(request, "email");
String email2 = request.getParameter("email");
String email3 = request.getAttribute("email").toString();
System.out.println("email1 : " + email1); //empty value
System.out.println("email2 : " + email2); //null
System.out.println("email3 : " + email3); //null
//etc.
}
I guess that the problems start here <form id="linkedInForm" action="<%= linkedInRegiserURL.toString() %>", but I am not sure and I don't know how should I pass my email parameter.
PS: I am working with Liferay 5.2.3, so writing a class extending BaseStrutsPortletAction is out of the question.
Params Inside login hooks in Liferay are a bit tricky, you can try 2 things:
Use the following function to retrive the "real" request wich may contains your parameter (Although I´´m not really sure if it´s available in liferay 5.2.3, in Liferay 6 its works):
PortalUtil.getOriginalServletRequest((javax.servlet.http.HttpServletRequest request)
Try with a GET call , instead a POST.
Another way to do it is to save the email as a cookie( in javascript) and then recover it in the autologin hook.
Hope it do some help...
I have a a JSP file in this format(two select tags)-
<%# page import="mypackage.*" %>
<all the main tags>...
<form>
<select> options... </select>
<select> options... </select>
<input type="submit" value="submit" />
</form>
<textarea></textarea>
There is a java method inside "mypackage" which should take arguments from the <select> tags after clicking on submit.
The method returns a string which I want to output in the <textarea>.
How do I go about this ?
Thanks a lot guys.
Send it as HTTP POST or HTTP GET to a servlet, and receive it via doGet() or doPost() methods. You can access it via HttpServletRequest.getParameter().
void doPost(HttpServletRequest request, HttpServletResponse response)
I see that you are importing the mypackage.* classes into your JSP. Indeed, you could include Java code inside your JSP and call the class directly. Something like:
<%
MyClass c = new MyClass();
String result = c.doSomething(request.getParameter("select"));
out.println("<textarea>" + result + "</textarea>");
%>
should be sufficient (but not good: the result should be escaped).
However, this code is not very maintainable and it can be done better (the answer of kaustav datta is one standard way of doing it).
It can be done in a more elegant way using the Spring framework's MVC part: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html
It needs some configuration at the beginning and takes some time to understand, but when you got it, it is very nice.
In your case, a controller of the following form would be sufficient:
#Controller
public class SelectController {
private final class MyClass c = new MyClass();
#RequestMapping(value="/select", method = RequestMethod.POST)
public String doSelect(#RequestParam("selection") final String selection, final ModelMap model) {
final String result = c.doSomething(selection);
modelMap.addAttribute("result", result);
return "yourJsp";
}
}
request.getParameterValues("select")
here getParameterValues is a method of the request interface which returns a string in argument of this method pass name of your controller
when you get value from text box use the method request.getParameter("name");
I've developed an HTML page that sends information to a Servlet. In the Servlet, I am using the methods doGet() and doPost():
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String id = req.getParameter("realname");
String password = req.getParameter("mypassword");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String id = req.getParameter("realname");
String password = req.getParameter("mypassword");
}
In the html page code that calls the Servlet is:
<form action="identification" method="post" enctype="multipart/form-data">
User Name: <input type="text" name="realname">
Password: <input type="password" name="mypassword">
<input type="submit" value="Identification">
</form>
When I use method = "get" in the Servlet, I get the value of id and password, however when using method = "post", id and password are set to null. Why don't I get the values in this case?
Another thing I'd like to know is how to use the data generated or validated by the Servlet. For example, if the Servlet shown above authenticates the user, I'd like to print the user id in my HTML page. I should be able to send the string 'id' as a response and use this info in my HTML page. Is it possible?
Introduction
You should use doGet() when you want to intercept on HTTP GET requests. You should use doPost() when you want to intercept on HTTP POST requests. That's all. Do not port the one to the other or vice versa (such as in Netbeans' unfortunate auto-generated processRequest() method). This makes no utter sense.
GET
Usually, HTTP GET requests are idempotent. I.e. you get exactly the same result everytime you execute the request (leaving authorization/authentication and the time-sensitive nature of the page —search results, last news, etc— outside consideration). We can talk about a bookmarkable request. Clicking a link, clicking a bookmark, entering raw URL in browser address bar, etcetera will all fire a HTTP GET request. If a Servlet is listening on the URL in question, then its doGet() method will be called. It's usually used to preprocess a request. I.e. doing some business stuff before presenting the HTML output from a JSP, such as gathering data for display in a table.
#WebServlet("/products")
public class ProductsServlet extends HttpServlet {
#EJB
private ProductService productService;
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Product> products = productService.list();
request.setAttribute("products", products); // Will be available as ${products} in JSP
request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
}
}
Note that the JSP file is explicitly placed in /WEB-INF folder in order to prevent endusers being able to access it directly without invoking the preprocessing servlet (and thus end up getting confused by seeing an empty table).
<table>
<c:forEach items="${products}" var="product">
<tr>
<td>${product.name}</td>
<td>detail</td>
</tr>
</c:forEach>
</table>
Also view/edit detail links as shown in last column above are usually idempotent.
#WebServlet("/product")
public class ProductServlet extends HttpServlet {
#EJB
private ProductService productService;
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Product product = productService.find(request.getParameter("id"));
request.setAttribute("product", product); // Will be available as ${product} in JSP
request.getRequestDispatcher("/WEB-INF/product.jsp").forward(request, response);
}
}
<dl>
<dt>ID</dt>
<dd>${product.id}</dd>
<dt>Name</dt>
<dd>${product.name}</dd>
<dt>Description</dt>
<dd>${product.description}</dd>
<dt>Price</dt>
<dd>${product.price}</dd>
<dt>Image</dt>
<dd><img src="productImage?id=${product.id}" /></dd>
</dl>
POST
HTTP POST requests are not idempotent. If the enduser has submitted a POST form on an URL beforehand, which hasn't performed a redirect, then the URL is not necessarily bookmarkable. The submitted form data is not reflected in the URL. Copypasting the URL into a new browser window/tab may not necessarily yield exactly the same result as after the form submit. Such an URL is then not bookmarkable. If a Servlet is listening on the URL in question, then its doPost() will be called. It's usually used to postprocess a request. I.e. gathering data from a submitted HTML form and doing some business stuff with it (conversion, validation, saving in DB, etcetera). Finally usually the result is presented as HTML from the forwarded JSP page.
<form action="login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="login">
<span class="error">${error}</span>
</form>
...which can be used in combination with this piece of Servlet:
#WebServlet("/login")
public class LoginServlet extends HttpServlet {
#EJB
private UserService userService;
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = userService.find(username, password);
if (user != null) {
request.getSession().setAttribute("user", user);
response.sendRedirect("home");
}
else {
request.setAttribute("error", "Unknown user, please try again");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}
You see, if the User is found in DB (i.e. username and password are valid), then the User will be put in session scope (i.e. "logged in") and the servlet will redirect to some main page (this example goes to http://example.com/contextname/home), else it will set an error message and forward the request back to the same JSP page so that the message get displayed by ${error}.
You can if necessary also "hide" the login.jsp in /WEB-INF/login.jsp so that the users can only access it by the servlet. This keeps the URL clean http://example.com/contextname/login. All you need to do is to add a doGet() to the servlet like this:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
}
(and update the same line in doPost() accordingly)
That said, I am not sure if it is just playing around and shooting in the dark, but the code which you posted doesn't look good (such as using compareTo() instead of equals() and digging in the parameternames instead of just using getParameter() and the id and password seems to be declared as servlet instance variables — which is NOT threadsafe). So I would strongly recommend to learn a bit more about basic Java SE API using the Oracle tutorials (check the chapter "Trails Covering the Basics") and how to use JSP/Servlets the right way using those tutorials.
See also:
Our servlets wiki page
Java EE web development, where do I start and what skills do I need?
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
Update: as per the update of your question (which is pretty major, you should not remove parts of your original question, this would make the answers worthless .. rather add the information in a new block) , it turns out that you're unnecessarily setting form's encoding type to multipart/form-data. This will send the request parameters in a different composition than the (default) application/x-www-form-urlencoded which sends the request parameters as a query string (e.g. name1=value1&name2=value2&name3=value3). You only need multipart/form-data whenever you have a <input type="file"> element in the form to upload files which may be non-character data (binary data). This is not the case in your case, so just remove it and it will work as expected. If you ever need to upload files, then you'll have to set the encoding type so and parse the request body yourself. Usually you use the Apache Commons FileUpload there for, but if you're already on fresh new Servlet 3.0 API, then you can just use builtin facilities starting with HttpServletRequest#getPart(). See also this answer for a concrete example: How to upload files to server using JSP/Servlet?
Both GET and POST are used by the browser to request a single resource from the server. Each resource requires a separate GET or POST request.
The GET method is most commonly (and is the default method) used by browsers to retrieve information from servers. When using the GET method the 3rd section of the request packet, which is the request body, remains empty.
The GET method is used in one of two ways:
When no method is specified, that is when you or the browser is requesting a simple resource such as an HTML page, an image, etc.
When a form is submitted, and you choose method=GET on the HTML tag. If the GET method is used with an HTML form, then the data collected through the form is sent to the server by appending a "?" to the end of the URL, and then adding all name=value pairs (name of the html form field and value entered in that field) separated by an "&"
Example:
GET /sultans/shop//form1.jsp?name=Sam%20Sultan&iceCream=vanilla HTTP/1.0 optional headeroptional header<< empty line >>>
The name=value form data will be stored in an environment variable called QUERY_STRING.
This variable will be sent to a processing program (such as JSP, Java servlet, PHP etc.)
The POST method is used when you create an HTML form, and request method=POST as part of the tag. The POST method allows the client to send form data to the server in the request body section of the request (as discussed earlier). The data is encoded and is formatted similar to the GET method, except that the data is sent to the program through the standard input.
Example:
POST /sultans/shop//form1.jsp HTTP/1.0 optional headeroptional header<< empty line >>> name=Sam%20Sultan&iceCream=vanilla
When using the post method, the QUERY_STRING environment variable will be empty.
Advantages/Disadvantages of GET vs. POST
Advantages of the GET method:
Slightly faster
Parameters can be entered via a form or by appending them after the URL
Page can be bookmarked with its parameters
Disadvantages of the GET method:
Can only send 4K worth of data. (You should not use it when using a textarea field)
Parameters are visible at the end of the URL
Advantages of the POST method:
Parameters are not visible at the end of the URL. (Use for sensitive data)
Can send more that 4K worth of data to server
Disadvantages of the POST method:
Can cannot be bookmarked with its data
The servlet container's implementation of HttpServlet.service() method will automatically forward to doGet() or doPost() as necessary, so you shouldn't need to override the service method.
Could it be that you are passing the data through get, not post?
<form method="get" ..>
..
</form>
If you do <form action="identification" > for your html form, data will be passed using 'Get' by default and hence you can catch this using doGet function in your java servlet code. This way data will be passed under the HTML header and hence will be visible in the URL when submitted.
On the other hand if you want to pass data in HTML body, then USE Post: <form action="identification" method="post"> and catch this data in doPost function. This was, data will be passed under the html body and not the html header, and you will not see the data in the URL after submitting the form.
Examples from my html:
<body>
<form action="StartProcessUrl" method="post">
.....
.....
Examples from my java servlet code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
String surname = request.getParameter("txtSurname");
String firstname = request.getParameter("txtForename");
String rqNo = request.getParameter("txtRQ6");
String nhsNo = request.getParameter("txtNHSNo");
String attachment1 = request.getParameter("base64textarea1");
String attachment2 = request.getParameter("base64textarea2");
.........
.........
I'm taking a class on JSP and I have an assignment... we have to write a JSP page that takes user input, validate the input and then forward it to a different web site. To be more precise, we were asked to implement a rudimentary version of the FareFinder functionality of Amtrak's web site.
There are 2 main purposes to this assignment:
(a) to write JSP which performs as middleware;
and (b) to write JSP which validates form data.
I have a general question about the principles of doing the validation. Currently I have a JSP that has a form and a submit button. When the user clicks on the submit button I forward them to Validate.jsp. The Validate.jsp will then validate the data and if the input is OK it will automatically redirect the request to the Amtrak web site with all the parameters filled out.
FareFinder.jsp -> Validate.jsp -> Amtrak
(click on the file name to see all my code in a pastie)
Briefly, the main thing that I'm doing FareFinder.jsp:
<FORM METHOD=POST ACTION="Validate.jsp">
<!-- all the input fields are up here -->
<P><INPUT TYPE=SUBMIT></P>
</FORM>
The main thing I'm doing in Validate.jsp:
<%# page import="java.util.*" import="java.io.*"%>
<%
// retreive all the parameters
String origin = request.getParameter("_origin");
String depmonthyear = request.getParameter("_depmonthyear");
String depday = request.getParameter("_depday");
String dephourmin = request.getParameter("_dephourmin");
String destination = request.getParameter("_destination");
String retmonthyear = request.getParameter("_retmonthyear");
String retday = request.getParameter("_retday");
String rethourmin = request.getParameter("_rethourmin");
String adults = request.getParameter("_adults");
String children = request.getParameter("_children");
String infants = request.getParameter("_infants");
String searchBy = request.getParameter("_searchBy");
// validate the data
// redirect to Amtrak or back to FareFinder.jsp
%>
I have several questions:
How do I return to FareFinder.jsp from Validate.jsp and reflect the errors found in the validation page?
Once I have found errors- do I redirect the response back to FareFinder.jsp?
How could I transmit the error(s) back to FareFinder.jsp?
A generic answer would be fine too, but I'm giving my code as an example.
Note: the validation must be performed on the server side and I can't use javascript.
OK, as per the comments, Servlets are covered as well. Now, create one and implement doPost() like follows (semi pseudo):
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String, String> errors = new HashMap<String, String>();
String origin = request.getParameter("origin");
if (origin does not validate) {
errors.put("origin", "Put error message here.");
}
// Repeat for all parameters.
if (errors.isEmpty()) {
// No errors, redirect to Amtrak.
response.sendRedirect("http://amtrak.com");
} else {
// Put errors in request scope and forward back to JSP.
request.setAttribute("errors", errors);
request.getRequestDispatcher("FareFinder.jsp").forward(request, response);
}
}
Map this servlet in web.xml on an url-pattern of /validateFare so that you can invoke it by http://example.com/context/validateFare.
Now do in JSP something like:
<form action="validateFare" method="post">
<label for="origin">Origin</label>
<input id="origin" name="origin" value="${param.origin}">
<span class="error">${errors.origin}</span>
<!-- Repeat other fields here. -->
</form>
You see that the form action already points to the servlet. The ${param.origin} in input values will retain the submitted value by doing request.getParameter("origin") under the hoods. The ${errors.origin} will show any associated error message by roughly doing pageContext.findAttribute("errors").get("origin").
This must get you started :) Good luck.