I have to pass parametrs From servlet to jsp .Iam using the following code.Is it possible to pass parameters through this way?
String val="Testvalue"
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp?valpass=val");
dispatcher.forward(request, response);
In jsp
String value=(String)request.getAttribute("valpass") ^
if you forward from servlet to jsp you should set as attribute do
request.setAttribute("key","value")
parameter is mainly used in communicating with client to server. and use attribute as internal message passing
Related
I've been through a few tutorials for java servlets and they all show how to display a web page generating in the java code using a servlet. How can I display an existing html page using a servlet?
I figure I need to do something with HttpServletRequest.getRequestDispatcher but not sure exactly what?
you can do this in two ways:
Request dispatcher
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/somePage.html");
rd.forward(request, response);
Response sendRedirect()
response.sendRedirect("/someUrl.html");
See the difference between the two methods here: RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()
I've a URL pattern like webroot/TellSomeoneMail and corresponding class,
<servlet>
<servlet-name>TellSomeoneMail</servlet-name>
<display-name>Tell Someone Mail</display-name>
<servlet-class>com.nightingale.register.servlet.TellSomeoneMailServlet</servlet-class>
</servlet>
but how to identify which JSP file calling this servlet?
You can identify during execution into our servlet by looking to the referer header in the HTTP body:
String referrer = request.getHeader("referer");
Edit 1: You can also use session to keep the last url acceded by the user (such mechanism is already present in framework like grails or Spring under the "flash" attribute, not to be confused with adobe flash). If you use simple Servlet / JSP, you need to code such support...
Edit 2 Last solution if cookie and referee is blocked, is to add a parameter in the URL with reference to the last page, for instance URL?from=home_pg or URL?from=/homepage.html but it could require rewriting of urls embedded in the page.
you can get the URL from which the request was sent. Take a look at the following code
if (request instanceof HttpServletRequest) {
String url = ((HttpServletRequest)request).getRequestURL().toString();
}
To find JSP pages that allow the user to make requests to your servlet : Check the path the servlet is mapped under in the <servlet-mapping> element(s) in web.xml.
Then do a full text search on all the JSP's in your projet for this string. Look for HTML <a> and <form> element with target containing your servlet path.
I have a bunch of JSP pages all of which are managed by a single servlet. I tried getting a JSP page name using the request's method getRequestURI for getting a full JSP path, but it returns only the path to the servlet (without any JSP page). Is there a way to retreive a JSP name from the HttpServletRequest? Thanks.
In case when I have in url:
http://localhost:8080/TutorWebApp/page/common/login.jsp
I want to get on servlet with help request /page/common/login.jsp
HttpServletRequest#getRequestURL?
If you get a url of the jsp, you can write the following in the jsp file.
<%= request.getRequestURL() %>
I am using a session scope to store the bean,and i want to project the bean value to the jsp page when needed like this way
request.getSession().setAttribute("bean", bean);
response.sendRedirect("test.jsp");
And in the jsp i am using the below code to get the value on jsp
<% bean1 bean = (bean1) session.getAttribute("bean");
%>
<%= bean.getValue() %>
Instead of using a session scope i want to use a request scope,so can i set my attribute in my servlet in this way
request.setAttribute("bean", bean);
So how can i call it on my jsp
can i say
<% bean1 bean = (bean1) request.getAttribute("bean");
But it is showing error.Or instead of using scriplet how can i show my output using JSTL.
You're not understanding what a redirect is. A redirect is a response you send to the browser so that the browser sends another, new request to the location you redirected to. So, when you call sendRedirect("test.jsp"), the browser will send a new request to test.jsp. And obviously, all the attributes you have stored in the current request won't be available anymore.
It's impossible, without context, to say if a redirect is something you should do in this case, or if you should instead forward to the JSP. A forward is very different from a redirect, since it only transfers the responsibility of the current request and response to another component. In that case, there would be a unique request, and the JSP could find the attribute set by the servlet in the request.
The only thing I can say is that, in a properly designed MVC application, the JSP is used as a view, and there should never be a direct request to the view. Each request should go through a controller.
This question already has answers here:
Generate an HTML Response in a Java Servlet
(3 answers)
Closed 6 years ago.
I have a servlet that processes some content from the web and generates a String value. I need to display this String value in a html page within a table tag.
How do I pass this string value from servlet using the setAttribute method and getrequestdispatcher method?
Thanks
Abhishek S
In your Servlet, set data as attribute in request:
RequestDispatcher dispatcher = request.getRequestDispatcher("yourJspPage.jsp");
request.setAttribute("Name", "Temp"); // set your String value in the attribute
dispatcher.forward( request, response );
In your jsp page, access the request attribute like this:
<table>
<tr>
<td><%=request.getAttribute("Name")%></td>
</tr>
</table>
Hope this helps!
You can pass the data from servlet to JSP (not HTML) using request forward and by setting data as attribute in request and then on JSP you can render those data to generate HTML
See
Servlet
JSP
First create a PrintWriter object, which will produce the output on HTML page.
Here response is HttpServletResponse object from doGet or doPost method.
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html-code>")
If you want to use table tag then you can do this as
out.println("<html><body><table>...your code...</table></body></html>");
The result will be displayed on HTML page.
Suppose you sent ajax get request from html using jquery.
This is in html script
$.get('HelloServlet', {a:'abc',b:'abc'}, function (data) {
alert(data);
});
This code in Servlet
String str = "abc";
PrintWriter out = response.getWriter();
out.write(str);
When your servlet successfully executes you get 'str' variable value in alert 'data' variable.
You can do this by passing the servlet value as HTML-JavaScript-content and then access that content in the script tag.
You can try this: In Servlet method
PrintWriter out = response.getWriter();
out.print("var xyz = 20;");
In HTML Page
Inside script tag:
var abc = xyz;
But you will have to execute the servlet in the HTML page.
In tomcat, if you have the servlet mapping just type:
"<\script src="/servlet-name"></script>