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>
<% } %>
Related
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.
I have 2 JSP's pages as well as 2 servlets.
I want to get what ever the user enter in the username text box and store it in 2 scriptlets and then show it on the 2nd JSP page.
Example:
JSP Page 1
<input type='text' name=user>
Servlet 1
String userName = request.getParameter("user");
Up to this point im good I understand.
But what if I want to carry over that same value to in this case a 2nd servlet and then from that servlet display the same value to another jsp page?
I tried setting and getting the attribute like this,
request.setAttribute("userName", userName);
RequestDispatcher dispatch = request.getRequestDispatcher("SecondServlet");
dispatch.forward(request, response);
And then casting to string in the 2nd servlet how ever when I place that value in a scriptlet tag it does not display anything on the jsp page?
I am sending two values by using Query string using both jsp and servlet pages.
I need to taking only one value from query string in jsp page working fine.
At the same time in servlet page when i click button i need to get the value
from query string but here i am not getting the value from query string.
which value is i need to took in jsp that value only it visible remaining value
is appear in servlet page.
how can i retrieving query string values in my servlet
In jsp i am using query string at the text box value code:
<input type="text" id="txtBatchName" name="txtBatchName" value="<%=request.getParameter("BatchId").replace("id=","")%>">
Servlet page:
if(request.getParameter("btnUpdate")!= null){
String Batch2=request.getQueryString();
String Id1=request.getParameter("Id");
}
Try with name attribute.(Id is specific to Java Script and it wont be transmitted to server side)
String Id1=request.getParameter("txtBatchName");
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.
This question already has answers here:
Assign JavaScript variable to Java Variable in JSP
(7 answers)
Closed 8 years ago.
I want to send code title and price as an argument to the book object but gives illegal start expression error.
function addrecord() {
code = document.getElementById('code').value;
title = document.getElementById('title').value;
price = parseFloat(document.getElementById('price').value);
<% Book book = new Book(%> code <% ,%> title <% , %> price <% );
bookdb.addRecord(book);
%>
document.getElementById("code").value = "";
document.getElementById("title").value = "";
document.getElementById("price").value = "";
}
You are misunderstanding where java and javascript run.
You use <% and %> as if that sends values to the server, which it does not. those tags are used only inside the jsp to start snippets of java code. This only works while you are generating the page. Once your html is at your browser, these tags will not do anything.
Highlighting important keywords, this is not intended as sarcarsm
When you request a jsp page with your browser, the browser opens a network connection to the server. Think of these 2 (browser, server) as living in 2 completely different places.
The server then runs the java code. When it executes, it generates an html page.
This html page is sent back across the network connection to the client (your browser)
Your browser will receive this fully formed html page and use it to create it's view. On this page you can run javascript but the javascript will only run on the client (browser)
To get any variable back to the server again, you will need to either: get or post a form (using a submit inside a form tage, or use JavaScript post request like a form submit) , do an ajax call (http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml) or have the user click a link (a href="...your page").
The easiest way is to do:
<form method="POST" action="your/page/url">
<input type="hidden" name="someName" value="someValue" />
<input type="submit" />
</form>
read some of the links and make sure you keep in mind, your browser and the server are separated, so running javascript in your browser will not reach the server.
See here: Assign JavaScript variable to Java Variable in JSP
As that states, it's really not possible as the JSP executes on the server, and the javascript executes later in the browser.