Here is the code in JSP:
<li>
<input type="hidden" name="testID" value="mot0000001">
Sample Test
</li>
Here LoadTestDetails is the name of servlet. The servlet is called but on printing the testID, through out.println(request.getParameter("testID")); i got output: null.
When you click on the link, your browser simply performs an HTTP GET request. The value of the hidden input field will not be sent along.
To get the effect you want, either submit a form with a javascript or append the value as a URL parameter like this:
<a href="${pageContext.request.contextPath}/LoadTestDetails?testID=mot0000001">
Sample Test</a>
Form submit has a side effect of disturbing the Browser navigation (back button), the URL-encoded parameter will be visible in the URL. Choose what you prefer.
Example form submit with Javascript:
<form id="myForm" action="${pageContext.request.contextPath}/LoadTestDetails"
method="POST">
<input type="hidden" name="testID" value="mot0000001">
<a href="#" onclick="document.getElementById('myForm').submit();">
Sample Test</a>
</form>
Related
here is my problem. How could I hide the value of the parameter from the url? because I don't have idea how to hide it. it keep on appearing like this (http://localhost:8084/YIP/MentorServlet?action=peribadi&mentorid=951218-02-5598)
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×
</a>
<% String id=request.getParameter("mentorid");%>
<li>
Utama
</li>
<li>
Peribadi
</li>
Some options:
do nothing: this is the best one, as there is no such thing as securely hiding something in HTML. Whoever looks into the page source, will see how the servlet in question can be invoked
switch to a form and a submit button, something what #alayor shows. If you use POST, the parameters will not appear in the URL
switch to a form, but keep the looks of an anchor and submit form from JavaScript (some docs some overcomplicated examples)
manipulate browser history from the target page (docs1, docs2)
keep mentorid in a session variable on server-side: hackers never see it
keep mentorid in an encrypted cookie: hackers see it, but can not decode. However they can try reusing it later (replay attack)
the various other ones I have forgotten and/or never even heard about
You can create an HTML for instead of an anchor.
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×
</a>
<% String id=request.getParameter("mentorid");%>
<li>
Utama
</li>
<li>
<form action="/MentorServlet" method="POST">
<input type="hidden" name="action" value="peribadi" />
<input type="hidden" name="mentorid" value="<%=id%>" />
<button>Peribadi</button>
</form>
</li>
This way you can avoid sending the parameter in the URL and it will send in the HTTP Request Body instead.
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
Is there a way to get the name of the form element itself using JSP? I searched google and did not find any examples which show how to get the name of the form value specified in the JSP file.
For example, I have the below form,
<html>
<form name="register" action="servregister" method="POST"
onsubmit="return validate();">
</form>
</html>
I would like to get the string "register" to a string variable in JSP. Is there a way to do this?
No, a <form> is not submitted with the request. Instead create a hidden input element which holds that information.
<input type="hidden" name="name of form" value="value" />
or possibly the submit element
<input type="submit" name="distinguishing name" value="submit" />
Either of these in a form with be sent as a url-encoded parameter.
There is probably a better solution to what you are trying to do if you explain your goals.
Consider looking into different patterns for achieving your goals.
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