Whenever I click on a button in my HTML side, I'm switching to my JSP-File (localhost:8080/index.hmtl -> localhost:8080/result.jsp). But my intention is to load the JSP script in the background and put the result of the jsp on my HTML side so i still stay on localhost:8080/index.html. Summarized this is a quick overview of my HTML side:
<form action="result.jsp">
Some Input textfield: <input type="text" name="name">
<input type="submit" name="button" value="eintragen">
</form>
So, is there a special command for this or a easy way to solve this problem?
Related
I have a JSP page which reads data from HTML and has CSS,Jquery code in it .
Now my webpage in jsp has two text labels and a add button next to them.
User can enter any no of values in the text field.
Now my requirement is that every time user enters the alue in these fields and clicks on add then that data should be passed on to my servlet. Servlet will basically do some validation and return a boolean variable.
Based on the value of this boolean, I shall change the appearance of my text boxes.
This is required to be done for every time user clicks on Add button.
How can I achieve this ?
My HTML code :
<div id="id1" name="id1" style="display: none;">Add a node: </br>
<input type="text" name="ipaddress" id="ipaddress" placeholder="Enter Node IP"> <input type="text" name="port" id="port" placeholder="Enter Node Port">
<input type="button" value="Add" name="addnodebutton" id="addnodebutton"/>
</div>
The value in ipaddress and port shall be passed on to my servlet and depending on return parameter, their appearance should change.
Can anyone enlighten me how this is actually going to work ?
TIA :)
For passing data to and from a servlet, you have options.
Option 1- You can wrap your html in a form tag and set the action/method properties for your servlet/http method like below:
<form method="POST" action="servletname">
<input type="text" name="ipaddress" id="ipaddress" placeholder="Enter Node IP">
<input type="text" name="port" id="port" placeholder="Enter Node Port">
<input type="submit" value="Add" name="addnodebutton" id="addnodebutton"/>
</form>
The submit would send a request with the input to your servlet. You would then need to handle your request parameters in your servlet, set your values/flags in your response object and forward to the user or jsp/html page of your choice.
Option 2- You can make an ajax call from your jsp, process your input and return a response to your page asynchronously. Example below:
A Simple AJAX with JSP example
I'm using eclipse. I have a login form like this
<form method="post" action="login">
<p>Login</p>
<input type="text" id="email"/><br>
<input type="text" id="password"/><br>
<input type="submit" value="GET STARTED"/>
</form>
when I use request.getParameter("email") in the servlet, it doesn't give me the input value because I don't specify the name of input. So I add the name attribute.
<form method="post" action="login">
<p>Login</p>
<input type="text" id="email" name="email"/><br>
<input type="text" id="password" name="password"/><br>
<input type="submit" value="GET STARTED"/>
</form>
But still I don't get the input value. Then after 1+ hour of invalid debugging, I exit the eclipse. Then I restart the eclipse again, the problem is gone.
I guess does it have something to do with "how the IDE mapping form input values" and stuff? I tried clearing the cache of eclipse, but it turned out not related to the issue.
Anyone can explain what happened here? Thanks!
Update in 0504:
It has something to do with the IDE's web browser, although I don't know exactly what the problem is. I change to use the external Chrome and it works fine there.
The second code should have worked here. Its the name attribute that gets posted in the form submit. I guess, you forgot to save or the saved code was somehow not deployed in the Application server.
As we know every jsp program there is a servlet behind the jsp page. I have used a jsp page to make a form (its a very small form), and in the same jsp i used scriptlet tags and made a way to get the inserted form data, and display it using out.print(). but the problem is it when i run it, the form is displayed., but when i submit is, it doesn't recognize the servlet page (error coming as "The requested resource is not available"). i will put the code below., please help me friends to solve this problem. thank you.
i did this in netbeans.
jsp page name is- "hello.jsp"
the servlet page name behind the jsp page is: "hello_jsp.java".
<html>
<head><title>IF...ELSE Example</title></head>
<body>
<form action="hello_jsp" method="post">
<input type="text" name="y"/>
<input type="submit" value="submit"/>
<%
if(request.getParameter("y")!=null) {
String s = request.getParameter("y");
if(s.equals("hello")){
out.print("welcome"+s);
}else{
out.print("not welcome");
}}
%>
</form>
</body>
</html>
My guess is that you need to change
<form action="hello_jsp" method="post">
to
<form action="hello.jsp" method="post">
<!-- ^---- change is here -->
The externally-accesible resource is the jsp, not the servlet. (By default, I'm sure some config-fu could change that.)
Or, of course, if the page is supposed to submit to itself, don't include action at all. The default is to submit to the current page.
<form method="post">
I have 2 jsp pages and one Servlet. I am fetching data from database by servlet and sending result to result.jsp page where i am displaying result. But i want to add a Back button in result.jsp , by clicking back button i want to go to index.jsp, but problem is when i am clicking back button everytime a message is coming Confirm form submission and it is irritating me. How can i avoid this Confirm form submission? perhaps it is coming as processing is done in servlet.
index.jsp
<form method="post" action="Student">
<input type="text" name="studentname"/>
<input type="submit" value="search"/>
</form>
Student servlet
String student_name=request.getParameter("studentname");
-------fetching data from database------------
-------------------sending to result.jsp------
String nextJSP = "/result.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);
result.jsp
//displaying data here
<form action="index.jsp">
<input type="submit" value="back"/>// a back button is here which is going to index.jsp
</form>
EDIT
From result.jsp i am going to another page result1.jsp like below
In result.jsp i have written the following:
<%out.println(student_name);%>
By clicking the above hyperlink i went to result1.jsp
I want to add a back button here(in result1.jsp) and after clicking i want to do to result.jsp, but when clicking back button i am getting Confirm form submission every time. I have written the following in result1.jsp
<input type="button" value="Back" onclick="javascript:history.go(-1)">
Still i am getting that message Confirm form submission. How can i avoid this? I want to go to result.jsp directly with out this message. How is it possible?
you can also use this to go one page back
<button type="button" name="back" onclick="history.back()">back</button>
You can write the below code that let's you to go index.jsp on your result.jsp page
Back
Try this
<button type="button"
name="back"
onclick='window.location='<your_path>/index.jsp'>back</button>
If you want a back button to go index.jsp, why not just make a normal link?
Back
There is only two way to get rid of the message, normal link or window.location. If the previous page is always the same, you don't need to use complicated function client side. If the page could be different, just post the link to result.jsp and use it to actually print a back link!
EDIT :
previous.jsp
<form method="post" action="Student">
<input type="hidden" name="back" value="previous.jsp" />
<input type="text" name="studentname"/>
<input type="submit" value="search"/>
</form>
result.jsp
out.println("Back");
This is the easiest way to create a goBack button using the method goBack(). This is the same as clicking the "Back button" used on the top of your browser.
<!DOCTYPE html>
<html>
<head>
<script>
/* The back() method loads the previous URL in the history list.
This is the same as clicking the "Back button" in your browser.
*/
function goBack() {
window.history.back()
}
</script>
</head>
<body>
<button onclick="goBack()">Go Back</button>
<p>Notice that clicking on the Back button here will not result in any action, because there is no previous URL in the history list.</p>
</body>
</html>
You can use a common button.
<input type="button" value="Back" onclick="javascript:history.go(-1)">
With history.go(-1), your browser will simply display previous page by reading from cache, it will not resubmit your data to the server, thus, no Confirm form submission will happen.
EDIT
I was wrong!
All you need is a client side redirect, you may write something like this in your result.jsp after it handles the form submitted from index.jsp:
response.sendRedirect("result.jsp");
So you will need a parameter to help you in result.jsp to tell whether it is a simple display request or a form submission, something like this:
String action = request.getParameter(action);
if("submit".equals(action)) {
// handle form data
response.sendRedirect("result.jsp");
} else {
// other code to display content of result.
}
In your index.jsp, it would be something like this:
....
<form action="result.jsp?action=submit" ...>
I experienced that form submit confirmation with Safari (not with IE/Chrome/FF).
The work around is submitting your form with "get" method. Of course this is only valid for "small" forms (max 2048 K).
Try this
<a href="${pageContext.request.contextPath}/index.jsp" class="btn btn-success">
Back
</a>
with bootstrap.css from here
Using IE 7, JDK 1.6 and Sun Web server 7.
Inside the jsp form, we have:
<input type="text" name="id" maxlength="20" />
<input ... type="submit" name="send" value="Send"/>
i.e. a text box and a "Submit" button (called Send).
and the servlet has:
if (request.getParameter("send") != null && request.getParameter("send").trim().length() > 0) { ... }
Using Fiddler and IE, we can see that the following is sent when we populate the id text box and hit Enter:
id=123456
However, using Fiddler and IE, we can see that the following is sent when we populate the id text box and click the Send button:
userId=123456&send=Send
The end result is that hitting the Enter key effectively does nothing.
On other jsp pages, e.g. we have:
<input type="text" name="id" maxlength="20" />
<input ... type="submit" name="submitId" value="Submit"/>
and the servlet has:
if (request.getParameter("submitId") != null && request.getParameter("submitId").trim().length() > 0) { ... }
Using Fiddler and IE, we can see that the following is sent for both cases:
id=123456&submitId=Submit
So it seems to us that the behaviour is only exhibited on IE for forms where the "Submit" button is not called "Submit"?
Re-running the tests on Firefox 3.6 shows that the behaviour is correct and the same for both cases.
Any suggestions for getting IE to work correctly?
(Note: I have searched SO for a similar problem but the questions relating to this mainly all ASP related!).
This is indeed another IE anomaly in case of forms with only one input field. The only solid workaround for this is to add a second input field(!). You can hide it using CSS. No, type="hidden" ain't going to work.
<input type="text" name="id" maxlength="20" />
<input type="text" style="display: none;" />
<input type="submit" name="send" value="Send"/>
Why are you checking for request.getParameter("submitId") in your JSP when in fact submitId is the name of your submit button?
In my experience I never had to check the value for the submit button. I only used that button to trigger the form submit and would usually only be interested in retrieving the values for the other form parameters.
If you want to differentiate the submit methods by the name of the submit button, you might want to try adding a "hidden" property using input type="hidden".