How to manipulate a variable before sending to servlet - java

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();
}

Related

Passing parameter back from JSP in a portlet app

In my Liferay 6 app I'm able to pass parameter from java to jsp via:
final PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("view");
request.setAttribute("description", "some description");
rd.include(request, response);
Then I want user to change the description and pass it back to back-end:
<form method="POST" action="${addItem}">
<input name="description"
type="text"
value="${description}"/>
<button type="submit">UPDATE</button>
</form>
Nevertheless when I call then System.out.println("request.getAttribute("description")); , I'm getting null. What am I doing wrong?
Youre passing in the parameter but checking the request attribute (assuming that the outer quotes are a question typo). Based on the information you provided, the initial request attribute was only available in the JSP but not any subsequent servlet. Try
System.out.println(request.getParameter("description"));

Why is this simple jsp program is NOT running?

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">

How to get form name value in JSP

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.

invoking java scriptlet in JSP using HTML

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.

in JSP is there a way to pass HttpServletRequest object as an attribute to another HttpServletRequest object

I am using Struts/JSP for a webapp. I have a page A where I am collecting user defined parameters (as request parameters can't make them session params) and then I go to page B to ask yes/no kind of a question to the user. In case of yes I need to come back to page A and continue regular processing. But obviously the request object for page A is gone.
Is there a way to set page A's request object as parameter in page B so that when I come back to page A i have the same request object I had when i was there (on page A) the first time.
I need something like below:
page A --(req1)------> page B (set req.setAttr('prevReq', req1)) ------> page A (req = req.getAttr('prevReq'))
Any help is appreciated.
No, you can't do what you have in mind. Do you understand how the HTTP request-response cycle works?
User sends HTTP request to the server using a browser.
Server processes the request (your servlet or JSP is called).
Your servlet or JSP produces a response which normally consists of an HTML page.
The server sends the response back to the browser.
There is no way that you can save the request for page A, and then in page B respond to that request to make the browser go back to page A. That's just not how the request-response cycle works.
What you can do, is store data in the session object. You can call request.getSession() to get a HttpSession object, in which you can store data for the duration of the session of that user. In page A, you can get the data out of the session object again.
In a multi page process you will need to store all the intermittently gathered data into the session. See HttpServletRequest.getSession() and HttpSession.setAttribute(String, Object).
Use hidden input elements (input type="hidden") wherein you retain the request parameters of the form submit. Don't duplicate/store it as request attribute. They get lost when the response is finished.
Since I don't do struts, here's a basic example how the JSP should look like (leaving input labels and obvious security issues like XSS outside consideration, Struts should be smart enough to handle it itself).
Page A:
<form>
<input type="text" name="input1" value="${param.input1}">
<input type="text" name="input2" value="${param.input2}">
<input type="text" name="input3" value="${param.input3}">
<input type="hidden" name="yesorno" value="${param.yesorno}">
<input type="submit" value="go to page B">
<input type="submit" value="submit">
</form>
Page B
<form>
<input type="checkbox" name="yesorno" value="yes" ${!empty param.yesorno ? 'checked' : ''}>
<input type="hidden" name="input1" value="${param.input1}">
<input type="hidden" name="input2" value="${param.input2}">
<input type="hidden" name="input3" value="${param.input3}">
<input type="submit">
</form>

Categories