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()
Related
I'm making a login/registration page using JSP, JSTL, and a Java servlet, and I'm using, in the doPost() method of the servlet:
request.setAttribute("message", "invalid login");
request.getRequestDispatcher("index.jsp").forward(request, response);
to alert the user when he enters invalid login credentials from index.jsp.
This works, but I don't know why it works; here are my questions:
Why is setAttribute() called on the request object and not the response object?
As I understand it, I'm forwarding the request and response objects to index.jsp, but how does a jsp page handle these objects? It's a bunch of html, so is the response object altered so that it includes all the html code of index.jsp?
Why is getRequestDispatcher() an instance method? That is, why can't RequestDispatcher objects be created using a constructor?
Thank you.
The HttpServletRequest is passed along until the response is finally handled. That is, when the JSP is being rendered, the request object is still available. This is how you pass arbitrary data throughout the whole request handling process.
JSP files are compiled to custom servlets that write the HTML to the response.
The RequestDispatcher is created from the request because it needs to know about the servlet environment. Otherwise it wouldn't know how to dispatch anything.
I have a Servlet which accept username and password and authenticates it.
After it is done, I want to display an HTML page.
It is a big HTML page so I read that using writer.println() was not a good idea.
How do I display the new page?
I tried using sendRedirect but that, for some reason, displayed the same page (login page) again.
Please help.
Create a JSP page contains your HTML, then use requestDispatcher to forward the request.
for Example:
if you created a JSP page named MyJSP, then in login Servlet use:
request.getRequestDispatcher("MyJSP.jsp").forward(request, response);
you can use this lines of code
create an html whatever the name you like i had created LoggedIn.html and
use this code in your servlet
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/LoggedIn.html");
rd.forward(request, response);
How to show response or output generated by servlet using separate .html file designed with CSS?
For example, output generated by servletresp.java in htmlpage.html.
Can we use CSS in Servlet programming?
You question is very generic so I'm going to assume you are a beginning Java web programmer.
To your first question, I advise you to use JSP pages (or any other template technology).
Yes, it is possible to serve .html files from a Servlet using a RequestDispatcher, but JSP pages are meant to generate such output - it is easy to make a small part of a JSP page dynamic, while serving HTML files from a servlet doesn't give you an option for some dynamic behaviour.
Just rename your file.html page to file.jsp and put in in your web source directory of your war project.
To your second question - HTML sent by a servlet or a JSP page is still normal HTML and you can use CSS as you would in any HTML page.
Code example:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/file.html");
// If you want to include the file in your response, use dispatcher.include -
// you can include multiple different files or send more output using
// response.getWriter() or response.getOutputStream
dispatcher.include(request, response);
// If you just want to send this one file as the response, use dispatcher.forward
dispatcher.forward(request, response);
}
If your aim is to have the user's browser (appear to) request a static .html page, but have your servlet code executed instead, there's a few ways you could do that.
Most (All? I'm only familiar with running Struts on WebSphere) servlet containers allow you to specify an arbitrary pattern for URLs that are mapped to actions, so you could simply put in a mapping for htmlpage.html to your servlet action that executes the associated code then runs through a JSP (or similar) to render HTML to be sent as the response.
Alternatively you could simply serve up a static HTML page that uses a JavaScript (so executed client-side) templating engine, then load the data for the page from your servlet (likely as JSON) via an AJAX call when the page loads.
The second part doesn't really make any sense. CSS isn't a programming language, it's simply rules for specifying how things look. It's also meaningless without something to interpret and apply those rules to the content (which is what the browser does). You can include CSS inside HTML, or a separate file, generated in your servlet code, but it's not going to do anything.
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
I need to call from one JSP to another, do some stuff over there..
I am inside the caller jsp, inside his handleReqeust(HttpServletReqeust request)
method I am trying to forward the request to another JSP, to call the other JSP file to his handleRequest(HttpServletReqeust request) off course with the request object
I tried it:
RequestDispatcher dispatcher = request.getRequestDispatcher("/theSecondJspFile.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);
but to make it work I need for it the object response, but I don't have it,
I am sure I missed something basic, but what?
From my question you can see, I don't have solid backround in java, so please correct me, or refer me to good guide if you feel it necessary
Thanks.
-------------------Edit--------------------------------------
I don't need a redirect, I just want to call another JSP file to his handleRequest method
I think it relate to HTML
What I can make out of your question is that you need to redirect to a secondpage.html from firstpage.html with some data. You can use both GET or POST method to send the data.
For the GET method just redirect to secondpage,html?data=value. The data will be available in the HttpRequest parameter in the controller of the secondpage.html where it can be used as required.
For the POST method you would need to post the data (using a form on firstpage.html) to secondpage.html. In the controller of the secondpage.html the data should be available in a similar way as before.
To include another JSP in a jsp :
<jsp:include page="foo.jsp"/>
You can find some reference material here.
I can not be sure, but what I think you are trying to do is to include a jsp page on to your jsp page and use the the objects and other variables declared in the first jsp page in your second.
<%# include file="mypage.jsp" %> should help you do this.
If this is not what you are looking for, please make your question tad bit more clear. some code will help really.
JSPs are supposed to be used to present the final result. JSPs are not supposed to be part of business tasks leading to this result. There you use normal Java classes for, starting with a servlet class. Let the HTTP request (the one which you enter in browser address bar or specify in some HTML link or form) point to the URL of the servlet instead. This way you can write Java code in the servlet the usual way to invoke other Java classes/methods and finally forward the request to a certain JSP file based on the outcome of the result and then just let that JSP present the final result.
To start with servlets, I'd suggest to read our Servlets info page.
I just added empty Iframe, and set his URL when I needed to call him