In my application I am trying to do a pageContext include in a custom tag java file. I am going to include a jsp file.
Can I pass any parameters to the jsp file which would be scoped only to the file(like params passed in jsp:include)?
I am aware that I can set it as an attribute in the request and use it in the jsp, however I don't think this is the best solution as I do not want it to persist in the request. Besides, the jsp file may in turn use the same tag again, which would override this attribute, and the rest of the jsp would have the wrong attribute.
Here is the code for that:
request.setAttribute("myVariable", this.viewletId);
pageContext.include(viewletLayout);
Is there a way to pass it to just the jsp in its scope(Like jsp:include)?
Look here to get the solution that hinted me to what worked for me:
<%
if ("layout1".equalsIgnoreCase(viewletLayout)){
%><%#include file="layout1.jsp"%><%
} else if ("layout2".equalsIgnoreCase(viewletLayout)){
%><%#include file="layout2.jsp"%><%
}
%>
I have a JSP page and a servlet for it. I'm trying to pass a parameter from the servlet to the jsp page and that works fine. Please note that this parameter or attribute is declared in another java class as static final.
But when I'm trying to pass this parameter again to servlet I see the value null.strong text
Variables.java
public static final String VAR_date="date";
JSP
String date=request.getAttribute(VAR_date); //this values is fetched
<A href='Servlet?VAR_date=<%=date%>'</A> //problem occurs here when sending date back to Servlet
Servlet
doGet(HttpServletRequest req,HttpServletresponse res)
String date=req.getParameter(VAR_date); // value is null
I dont know if the Syntax is different when you pass a variable which has been declared in java file, or if this is wrong.
Thank you in advance!
You have to either pass it to a field, and submit it (it can be hidden as well), which's name should be the VAR_date. Then you can read it in the servlet. Or alternatively, you can set it into session, and read it in the servlet (then possibly remove it), but I'd not recommend that.
The input thing looks like this:
<input type="hidden" name="<%= Variables.VAR_date =>" value="anything you want to submit" />
in your Servlet, add statement for importing Variables class and then directly access the required variable as follows:-
Variables.VAR_date
still if you want to pass from JSP then set date value in some hidden variables and then get hidden variables' values from those hidden parameters.
What is the difference between getAttribute() and getParameter() methods within HttpServletRequest class?
getParameter() returns http request parameters. Those passed from the client to the server. For example http://example.com/servlet?parameter=1. Can only return String
getAttribute() is for server-side usage only - you fill the request with attributes that you can use within the same request. For example - you set an attribute in a servlet, and read it from a JSP. Can be used for any object, not just string.
Generally, a parameter is a string value that is most commonly known for being sent from the client to the server (e.g. a form post) and retrieved from the servlet request. The frustrating exception to this is ServletContext initial parameters which are string parameters that are configured in web.xml and exist on the server.
An attribute is a server variable that exists within a specified scope i.e.:
application, available for the life of the entire application
session, available for the life of the session
request, only available for the life of the request
page (JSP only), available for the current JSP page only
request.getParameter()
We use request.getParameter() to extract request parameters (i.e. data sent by posting a html form ). The request.getParameter() always returns String value and the data come from client.
request.getAttribute()
We use request.getAttribute() to get an object added to the request scope on the server side i.e. using request.setAttribute(). You can add any type of object you like here, Strings, Custom objects, in fact any object. You add the attribute to the request and forward the request to another resource, the client does not know about this. So all the code handling this would typically be in JSP/servlets. You can use request.setAttribute() to add extra-information and forward/redirect the current request to another resource.
For example,consider about first.jsp,
//First Page : first.jsp
<%# page import="java.util.*" import="java.io.*"%>
<% request.setAttribute("PAGE", "first.jsp");%>
<jsp:forward page="/second.jsp"/>
and second.jsp:
<%# page import="java.util.*" import="java.io.*"%>
From Which Page : <%=request.getAttribute("PAGE")%><br>
Data From Client : <%=request.getParameter("CLIENT")%>
From your browser, run first.jsp?CLIENT=you and the output on your browser is
From Which Page : *first.jsp*
Data From Client : you
The basic difference between getAttribute() and getParameter() is that the first method extracts a (serialized) Java object and the other provides a String value. For both cases a name is given so that its value (be it string or a java bean) can be looked up and extracted.
It is crucial to know that attributes are not parameters.
The return type for attributes is an Object, whereas the return type for a parameter is a String. When calling the getAttribute(String name) method, bear in mind that the attributes must be cast.
Additionally, there is no servlet specific attributes, and there are no session parameters.
This post is written with the purpose to connect on #Bozho's response, as additional information that can be useful for other people.
The difference between getAttribute and getParameter is that getParameter will return the value of a parameter that was submitted by an HTML form or that was included in a query string. getAttribute returns an object that you have set in the request, the only way you can use this is in conjunction with a RequestDispatcher. You use a RequestDispatcher to forward a request to another resource (JSP / Servlet). So before you forward the request you can set an attribute which will be available to the next resource.
-getParameter() :
<html>
<body>
<form name="testForm" method="post" action="testJSP.jsp">
<input type="text" name="testParam" value="ClientParam">
<input type="submit">
</form>
</body>
</html>
<html>
<body>
<%
String sValue = request.getParameter("testParam");
%>
<%= sValue %>
</body>
</html>
request.getParameter("testParam") will get the value from the posted form of the input box named "testParam" which is "Client param". It will then print it out, so you should see "Client Param" on the screen. So request.getParameter() will retrieve a value that the client has submitted. You will get the value on the server side.
-getAttribute() :
request.getAttribute(), this is all done server side. YOU add the attribute to the request and YOU submit the request to another resource, the client does not know about this. So all the code handling this would typically be in servlets.getAttribute always return object.
getParameter - Is used for getting the information you need from the Client's HTML page
getAttribute - This is used for getting the parameters set previously in another or the same JSP or Servlet page.
Basically, if you are forwarding or just going from one jsp/servlet to another one, there is no way to have the information you want unless you choose to put them in an Object and use the set-attribute to store in a Session variable.
Using getAttribute, you can retrieve the Session variable.
from http://www.coderanch.com/t/361868/Servlets/java/request-getParameter-request-getAttribute
A "parameter" is a name/value pair sent from the client to the server
- typically, from an HTML form. Parameters can only have String values. Sometimes (e.g. using a GET request) you will see these
encoded directly into the URL (after the ?, each in the form
name=value, and each pair separated by an &). Other times, they are
included in the body of the request, when using methods such as POST.
An "attribute" is a server-local storage mechanism - nothing stored in
scoped attribues is ever transmitted outside the server unless you
explicitly make that happen. Attributes have String names, but store
Object values. Note that attributes are specific to Java (they store
Java Objects), while parameters are platform-independent (they are
only formatted strings composed of generic bytes).
There are four scopes of attributes in total: "page" (for JSPs and tag
files only), "request" (limited to the current client's request,
destroyed after request is completed), "session" (stored in the
client's session, invalidated after the session is terminated),
"application" (exist for all components to access during the entire
deployed lifetime of your application).
The bottom line is: use parameters when obtaining data from the
client, use scoped attributes when storing objects on the server for
use internally by your application only.
Another case when you should use .getParameter() is when forwarding with parameters in jsp:
<jsp:forward page="destination.jsp">
<jsp:param name="userName" value="hamid"/>
</jsp:forward>
In destination.jsp, you can access userName like this:
request.getParameter("userName")
Basic difference between getAttribute() and getParameter() is the return type.
java.lang.Object getAttribute(java.lang.String name)
java.lang.String getParameter(java.lang.String name)
i want to use JSP values in servlet. iam fetching some parameters from the url and i need to pass those parameters in my servlet and also the servlet result something by using those parameters and i want to use the result again in the same jsp and in other jsp.
i get those parameters first time when my home jsp is called.
i am getting url parameter by using jstl core tags:
<c:set var="data" scope="session" value="${param.urldata}" />
How do i do this using JSP tags???
Thanks in advance
As i understand somewhat , you are trying to send the values from JSP page to Servlet..
For this we have lot of solution
Pass the value through method
Set the value at session and get it wherever you want in the whole application
I prefer second point, because which hold the value for the entire application
Check Pass variables from servlet to jsp
Above question pass the value from servlet to JSP,Your question is opposite of the answer that's it..!
Set value at JSP : session.setAttribute("key","value");
Get value at Servlet: session.getAttribute("name");
Also refer : Sharing values between servet and JSP
In Expression Language, I can access my model like so: ${model.member}
How do I achieve the same thing when I want to use <%=some_method(${model.member}); %>
The reason is because I have some HTML helper methods I created to separate logic from UI, and I need to pass a member of the model to create the user control.
The JSP's main method has the following signature:
_jspService(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException
Based on this, you can access the request and response objects programattically from a scriptlet. For example:
<%= request.getParameter("foo").toString() %>
or
<%= request.getAttribute("bar").toString() %>
If you want to do something more complication, you could precede these with scriptlets to declare / initialize local (Java) variables; e.g.
<% String foo = request.getParameter("foo") == null ?
"no foo" : request.getParameter("foo").toString(); %>
<%= foo %>
You can use this to lookup your model in the request or response object (I think it will be an attribute of the request with name "model"), cast it to the appropriate type, and call its getter methods.
The reason is because I have some HTML helper methods I created to separate logic from UI, and I need to pass a member of the model to create the user control.
A better idea would be to turn those helper methods into custom JSP tags so that you can use them without resorting to scriptlets. JSPs with embedded scriptlets are generally thought to be hard to read and hard to maintain. One small mistake (or one change to the model API) and the JSP generates bad Java on your deployment platform and you get a broken page.
Take a look at JSTL custom functions. It allows a way for you to call static functions from your code in a JSTL standard way. You just need to set then up in your tld file.