How to prevent form inputs clearing in servlets? [duplicate] - java

This question already has answers here:
How can I retain HTML form field values in JSP after submitting form to Servlet?
(2 answers)
Closed 6 years ago.
I am creating a dynamic web project in eclipse using servlets.
Once I submited the form with username and password fields in index.html page. When username or password is incorrect, the servlet class should display the index.html page again with the data entered in the input fields except password fields, but it is not showing any data in input fields.

You need to set the username and password in response and read it in jsp.
You need to make jsp to display values.
You can set Object using
eg request.setAttribute("cust",customer);
Read above object in jsp and display values in jsp.

Related

Place value in an input field i.e., in a textbox in an html form from a servlet [duplicate]

This question already has answers here:
How can I retain HTML form field values in JSP after submitting form to Servlet?
(2 answers)
Closed 5 years ago.
What I'm trying to do here is to redirect to an html page with a form from servlet and as I redirect, is it possible to set values in a textfield of that form? Like the way I get the values from the form using request.getAttribute(), similarly is there a way to set values in a input element in an html form from a servlet?
Would greatly appreciate your help, thank you.
Java does not provide method to set parameters in servlet.
If you must do this using HTML here are two options:
(I still recommend using JSP)
1.display the registration page using PrintWriter#println() in servlet.
response.setContentType("text/html");
out.println("blah blah blah");
out.println("<input name='username' type='text' value='"+javaVariable+"'");
out.println("blah blah blah");
2.In your HTML page make an AJAX call to servlet that returns data you need and populate form elements with this data.

How to display data on static HTML page from JAVA servlets

I am very new to JAVA Servlets so pardon me if the question sounds silly. Basically, I have simple static HTML page having a form with input fields accepting values from a user. When a user enters values and click submit button values are sent to JAVA servlet where it does validation for values. If validation is failed I want servlets to send the old values which are correctly entered in addition to an error message for the invalid field and this will be displayed in static HTML page. How can I accomplish this with static HTML page and Java servlet?
I understand and totally agree that JSP would make my life easier in such case. However, if I have to use only HTML static page and JAVA servlet what are the options available?
You can include Javascript in the static web page that is called by the submit button onclick event and makes an AJAX call to the servlet. The servlet should respond with JSON or XML containing the validation results/error message. The form fields and/or error message text can be updated in the response handler Javascript function.
There are several examples on StackOverflow including this one. Scroll down to "Ajaxifying an existing form".

How to send error message from servlet to jsp page? [duplicate]

This question already has answers here:
How perform validation and display error message in same form in JSP?
(3 answers)
Closed 5 years ago.
My jsp page consists of a form that takes in an email address. I then use java to check if my postgresql database already contains that email address. Right now my servlet just redirects the user back to the Registration page if the email address already exists.
What I am trying to do is write an error message on the form "The email address you entered already exists" with java. I haven't figured out how to do it.
Is there some way I could do this with jQuery form validation? I am using that right now for required fields.
Here you need to pass information from servlet to JSP.
To Pass parameter from servlet to JSP or vice versa.
Use Session to pass value from one page to another (here).
In Servlet
request.getSession().setAttribute("email", "exist/notexist");
In JSP
Make a blank div just beside the email textbox and set its content based on the value of String s1 which is set from session.getAttribute("key") method.
<%! String s1 = ""; %>
<% s1 = (String) session.getAttribute("email");%>
<% if(s1.equals("exist")){ %>
<div class="besideemailbox" style="color : red">Email Already exist</div>
<% }else if(s1.equals("something")){ %>
<div class="besideemailbox" style="color : green">ok or a tick</div>
<% } %>

How to maintain the contents in the text field after redircting to same page jsp.

Hi I have jsp page That is having some text fields,I will fill that text fields and I will submit that form to server side.If any error comes in the server side I will redirect to the same page with error message Now I want that text field to remain as it is but in my case it is clearing.How to make the text field as same.
The two options available to you are:
Don't reload the page. Instead submit the data via AJAX or validate it by javascript. That way you don't ever have to touch the data in the form. Make sure your javascript highlights the fields that have errors in some way.
The endpoint that you're POSTing your data to needs to be able to recognise that the data is invalid and include that data when returning the user to the same page. I'm not familiar with jsp, but generally you'd do that by including variables in your template for your form that could contain the data, and passing empty strings on the first load of the page. If the page is then shown to the user again after a failed form validation, pass back the POST data that you received in your form request.
There are two option, you can dispatch the request/response to the same page instead of redirect, but you need to add an attribute and recover it in the JSP, or you can add the attribute in the session , recover the value to de text field and remove it using (if you are using JSTL)

Send contents of jsp page as an email Using Java - without servlet programming

I have an application to send the sql query results as an email.
I know how to send static contents of a jsp page in mail using javamail.
But i need to include my query results to the jsp page.
I have a jsp page "email.jsp"
In My java class i have fetched the records. I need to set the values in
List<DBValues> values = getRowsFromDB();
I need to iterate this list using jstl in my jsp page and then send the jsp page.
Could someone help me on this.
Please ask me if question is not clear.

Categories