Hi guys,

I need an advice on a html-refreshing issue

I have one html document containing three different forms with three submit buttons for each form.
The first form has some input fields for personal information (name,surname, age etc). When the user fills in the fields, presses the submit button, all the information is send to the DB and saved there. The problem is, when the user presses the submit button, the according .jsp for the form is activated and I loose my html site (it gets redirected to a blank white page).
How can I make it not jump to the blank page, but to stay on the same HTML site, execute the JSP and give a short feedback like "your data has been saved". Then the user can go on to fill the next form - the contact details.

There, the DB should recognize that this is the same user and save his contacts accordingly with the same userID. Here a second question:How do I manage this with seassions or with passing variables?


Any any help is much appreciated.

thanks!

I assume that the according.jsp is an empty jsp that has only DB code and nothing else.
Then when you are done, you can write code using the <forward> tag to redirect to the initial page. You can also send the data saved and display them. This time you can also make the fields readonly.

The above might solve your problem but it is not correct. Instead of going to the according.jsp go to a servlet, do the insert there and then redirect to the inital page using the RequestDispatcher. Also you can send a flag indicating that the first form has been filled and no more is needed.


initialPage.jsp

String form1Saved = request.getParameter(form1Saved);

<%
if (form1Saved==null) {
%>
  display the form with buttons and fields
<%
} else {
%>
   // display only the data send from the servler
<%
}
%>

Or you can skip the above if-statement and simply have the form pre-filled with data send from the servlet:

// surname send from servlet. If this is the first time the page loads then 
// surname would be null
String surname = request.getParameter("surname");
if (surname==null) surname="";

<form action="nameOfServlet1"> // or your jsp
  <input type="text" name="surname" value="<%=surname%>" />

  <input type="submit" name="submit1" value="Submit" />
</form>

OR
Use Ajax, preferably with jQuery which makes it easier

And check the tutorial about MVC connectivity on top of the jsp forum

Hi JavaAddict,

thank you for the detailed answer!! I would kindly ask you to somehow specify this part though:

Instead of going to the according.jsp go to a servlet, do the insert there and then redirect to the inital page using the RequestDispatcher.

It sounds very plausible to me, but I wouldn't know how to actually accomplish it

I thought the according.jsp is the servlet itself? And how exactly do I redirect back to the HTML containing the form?


Let me show you a sample part of the HTML structure :

<form method="post" action = "insertArtist.jsp" >
 < input type="text" name="name" />
 < input type="text" name="surname" />
 < ... some other input information > 
 < input type="submit" name="submit_personal" value="forrward me!" />
</form>

<form method="post" action = "insert_contacts.jsp" >
  < input type="text" name="telephone" />
  < ..... />
  < input type="submit" name="submit_contacts value="save contacts" />

About my JSPs: since I`m using Eclipse as developing environment, when I choose "new JSP" it comes with the whole HTML-structure. So it looks like this

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<head> ... </head> 
<body> 
   <%
     [I]here my JSP-instructions[/I]
   %> 
</body> etc.

So yes, I`m absolutely new to all this, so please excuse me for the "lame" questions. I come from the "let`s make it look good" side of the things and only now I try to dig deeper in the code-part.
It is very annoying to explain stuff to novice, I know....

Thank you in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.