send parameter from jsp to servlet using jsp:useBean - java

I am beginner in java and trying to figure out how to send parameter from jsp to servlet using this
inside my jsp -
<%! String option;%>
<% option = request.getParameter("option"); %>
<jsp:useBean id="processBean" scope="session" class="helpers.ProcessBean" />
${processBean.processRequest(option)}
I want to send 'option' to processRequest but this way it is always empty string, so I am doing something wrong.Could not find anything online.
Any help is highly appreciated.

Variable declared/set on the page's scripting language is not automatically available from EL. You can manually make it available via:
<c:set var="option" value="<%= option %>" />
before using it in EL.

Related

Evaluating dynamic EL expressions at runtime in JSP page

I am putting together an application and would like to apply "Custom" EL Expressions to sections on the page when it is rendered. This will help me control what parts are displayed on the page given the status of the page. The problem is that the EL Expressions are not being evaluated and treated only as String values. For example
<c:if test="${form.conditionEL}" >
<input type="submit" value="Close">
</c:if>
The value of the ${field.conditionEL} might evaluate to something like "submission.status eq 'COMPLETE'".
So, is there a way to get that string value to be interpreted within the JSP page ?
Thanks for any help in making this happen. Let me know if I missed providing any pertinent information to help resolve.
Environment: Java 8, Tomcat 9, JSP 3.0, Spring
Regards,
Mike
I would simply add the submission status to the page context which let's you simply do:
<c:if test="${submissionStatus eq 'COMPLETE'}" >
<input type="submit" value="Close">
</c:if>
Check whether EL expressions are ignored in your page tag. I've also faced the same issue and this resolved mine.
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isELIgnored="false" %>

calling Java method from jsp page

I have jsp page that imports Testing.java
<jsp:useBean id="test" scope="session" class="Testing" />
<jsp:useBean id="sb" scope="session" class="SessionBean" />
<jsp:useBean id="eb" scope="session" class="ErrorBean" />
I need to call public method that is in Testing class after user confirms changes.
this is what I have so far:
<tr>
<td align="left">
<a href="<%=test.persistPrintingInfo(eb,sb) %>" >
<img src="../images/update.gif" OnClick="if( !confirm('Update printing information?')) return false"></a>
</td>
</tr>
Does anyone know how to do this?
Can I maybe call javascript method and call persistPrintingInfo() through javascript?
the page has been sent by the server to your browser. while javascript can modify the content of your page , in order to call a bean's method you must make a call to the server(a request to the servlet) beacause the bean lives on the server side. and this call can be made by creating an url mapped to the servlet, or a form whose action is the servlet
`<FORM ACTION="${pageContext.request.contextPath}/sampleServlet">`
if the form's method is GET, then on the doGet() method of the servlet you call your bean's method.
this form does not need to contain any kind of field. it is created just to make a request to the servlet. while you would normally click the submit button to proceed to the action, this time we will submit the form through javascript. with some javascript tricks, i think this form can also be hidden, because you don't actually need it to be displayed in your page
so you simply create this form in your jsp, and submit it through javascript , like this:
on your link, you will have onClick=myJavascriptMethod(); in your jsp, you create a javascript block
<script type="text/javascript">
function myJavascriptMethod)=()
{
document.forms["myform"].submit();
}
</script>
You can use this way, although there is better approaches using servlets.
<%com.example.Testing.yourMethod()%>
a second approach which i found while googling is this one:
How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
in your case, the code will be
<img.. >
the newPage.jsp will contain just
<%yourPackage.YourClass.yourMethod()%>

Where can the variable in jsp be defined except java classes?

Suppose I have the following code:
<c:when test="${isFoo}">
Where can isFoo be defined except:
java classes
<c:set ... /> construction ?
You can also set it using c:set. For instance:
<c:set var="isFoo" value="true" scope="page" />
In JSP itself by scriptlets. E.g. as a request attribute:
<% request.setAttribute("isFoo", true); %>
Scriptlets are however discouraged since over a decade.
By an implicit EL variable. E.g. as a request parameter:
http://example.com/page.jsp?isFoo=true
Which can be accessed by ${param}:
<c:if test="${param.isFoo}">
There are many more, you can find them all in this overview.
See also:
Our EL (Expression Language) tag wiki page

JSP: Use information from one page to another

I currently have a JSP page with a Form for the user to enter their name, but what I want is to get the user forwarded to a different JSP page after form submission and to carry on their name to be used.
I don't want to use JSTL EL just simple JSP uses.
I was thinking of using a bean storing the detail in a session but how would it work.
Thanks.
You'd have JSP enter the info into a form and POST it to a servlet. The servlet would validate the form input, instantiate the bean, add it to session, and redirect the response to the second JSP to display.
You need a servlet in-between. JSPs using JSTL are for display; using the servlet this way is called MVC 2. Another way to think of it is the front controller pattern, where a single servlet handles all mapped requests and simply routes them to controllers/handlers.
duffymo you have the best idea but here is a quick solution by just passing things through the JSP.
Here is the JSP with the form
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Simple jsp page</title></head>
<body><form name="test" action="./stackTest2.jsp" method="POST">
Text Field<input type="text" name="textField">
<input type="submit">
</form> </body>
</html>
and then the second page looks like this:
<html>
<head><title>Simple jsp page</title></head>
<body><%=request.getParameter("textField")%></body>
</html>
And then put information in a hidden field, you can get information by using the request.getParameter method. This just prints out what was in the form, but using the same idea for inputting it in to the hidden field in a form. I recommend this as all my experience with sessions have ended in a failure. I STRONGLY DO NOT Recommend this method, MVC is a much better way of developing things.
Dean

Using jsp:param / c:param in a Portlet Environment

I'm trying to include JSP pages with jsp:param in a Portlet environment (using the Pluto portlet container).
for example,
<jsp:include page="test.jsp">
<jsp:param name="foo" value="bar"/>
</jsp:include>
and in test.jsp,
<c:out value="${foo}"/> or <%= request.getParameter("foo") %>
The output is always null and i've also tried using c tags, but got the same result.
<c:import url="test.jsp">
<c:param name="foo" value="bar"/>
</c:import>
I've searched through the net and many people have faced the same problem, except that there is no solution to it.
Is this a limitation or is there a different way to do it?
This works fine in a normal Servlet environment, but I see from a bit of googling that the portlet environment seems to break it. This is a shame, but indicative that the portlet spec is, to put it bluntly, broken.
If <jsp:param> won't work for you, the alternative is to use request attributes instead:
<c:set var="foo" value="bar" scope="request"/>
<jsp:include page="test.jsp"/>
And in test.jsp:
<c:out value="${requestScope.foo}"/>
or maybe just:
<c:out value="${foo}"/>
It's not as neat and contained as using params, but it should work for portlets.
I had the same problem. My solution was to work with Portlet's renderRequest object (which is accessible from included jsp files). In my portlet I set the attribute on the RenderRequest object then in my JSP (included via jsp:include). I use Portlet API to access the implicit renderRequest object. Here is a sample:
MyPortlet:
public void doView(RenderRequest request, RenderResponse response) {
request.setAttribute("myBean", new MyBean());
getPortletContext().getRequestDispatcher("myMainJSP.jsp").include(request, response);
}
myMainJSP.jsp:
<jsp:include page="header.jsp"/>
header.jsp:
<%# taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<% page import="sample.MyBean" %>
<portlet:defineObjects/>
<%
MyBean myBean = (MyBean)renderRequest.getAttribute("myBean");
%>
... html code...

Categories