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">
Related
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?
I'm trying to access form data that is filled out inside a jsp:included page from outside the page. I am using jquery and am open to using ajax. My code looks a little like this:
<form name=form1>
<jsp:include page="someFormData.jsp" />
//Other inputs out here in <% include %> files
<input type=button value=Submit onClick="return commonSubmit()"
</form>
I need to use the jsp:include style include because having everything on one page using
<%include...%> was causing an exception due to my jsp being too large. Alls I need to do is be able to submit the form and include the data inside "someFormData.jsp"
I am trying to find a way to invoke a piece of java code within the JSP using HTML form
<form method="get" action="invokeMe()">
<input type="submit" value="click to submit" />
</form>
<%
private void invokeMe(){
out.println("He invoked me. I am happy!");
}
%>
the above code is within the JSP. I want this run the scriptlet upon submit
I know the code looks very bad, but I just want to grasp the concept... and how to go about it.
thanks
You can use Ajax to submit form to servlet and evaluate java code, but stay on the same window.
<form method="get" action="invokeMe()" id="submit">
<input type="submit" value="click to submit" />
</form>
<script>
$(document).ready(function() {
$("#submit").submit(function(event) {
$.ajax({
type : "POST",
url : "your servlet here(for example: DeleteUser)",
data : "id=" + id,
success : function() {
alert("message");
}
});
$('#submit').submit(); // if you want to submit form
});
});
</script>
Sorry,not possible.
Jsp lies on server side and html plays on client side unless without making a request you cannot do this :)
you cannot write a java method in scriptlet. Because at compilation time code in scriptlet becomes part of service method. Hence method within a method is wrong.
How ever you can write java methods within init tag and can call from scriptlet like below code.
<form method="get" action="">
<input type="submit" value="click to submit" />
</form>
<%
invokeMe();
%>
<%!
private void invokeMe(){
out.println("He invoked me. I am happy!");
}
%>
Not possible.
When the form is submitted, it sends a request to the server. You have 2 options:
Have the server perform the desired action when the it receives the request sent by the form
or
Use Javascript to perform the desired action on the client:
<form name="frm1" action="submit" onsubmit="invokeMe()"
...
</form>
<script>
function invokeMe()
{
alert("He invoked me. I am happy!")
}
</script>
You can't do this since JSP rendering happens on server-side and client would never receive the Java code (ie. the invokeMe() function) in the returned HTML. It wouldn't know what to do with Java code at runtime, anyway!
What's more, <form> tag doesn't invoke functions, it sends an HTTP form to the URL specified in action attribute.
I've got a JSP page which contains a textbox, wrapped in a form. This form's action is set to a servlet.
I would like to manipulate the string (from the user's input in the textbox) before it is sent to the servlet, thus basically carrying out a simple request.setParameter call from the JSP to the servlet. Can this be done? If so how can I obtain the textbox's value in the JSP?
<form action="MyServlet" method="post">
<input type="text" name="txtUsername"/><br/>
<input type="submit" value="Submit"/>
</form>
You cannot do this using JSP code.
Remember, a JSP is processed, outputting its contents to the browser; that's where the JSP's request/response cycle ends.
Your options are:
Using JavaScript.
Using a Filter: http://docs.oracle.com/javaee/5/api/javax/servlet/Filter.html
Call a Javascript function on submit e.g. below:
function fnSubmit(){
document.getElementById("txtUsername").value = "new Value";
document.forms[0].submit();
}
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