I'm in a Tomcat and have a String with JSP Content. And I try to get the HttpServletResponse means the HTML Output. In normal case you call the JSP and the WebContainer translate it to an Servlet and generates the Output.
But I have no JSP as File, just a String with the Content of an JSP. Is there a Class or Factory where I can put the Stream and get it processed?
thanks in anticipation
No. there is not readily available solution for this.
But, you can try out this:
Write the JSP string to a file inside the web application content root. Let's say /tmp/jspstring.jsp. Use getServletContext().getRealPath("/tmp/jspstring.jsp") to get the path of the new jsp.
Using RequestDispatcher include the newly created JSP, such that the server will process the JSP using
Related
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 need to get the content of a jsp page from a servlet without redirect or forward, like a file for security issues, I'll process the HTML and write the new generated HTML in the servlet response. How can I do that?
An example of what I'm trying to do:
if (request.getParameter("pageName").equals("index")) {
//get index.jsp content and process it...
}
You can't do that in a servlet, but you could do it in a Filter, in the doFilter method. You would need to provide a wrapper around the response object, something like what they do here.
I'm using netbeans to create a web application that allows a cell phone user to scan a QR code and retrieve a journal's previous/next title information. The take home point is that there's no form that the user will input data into. The QR code will contain the search string at the end of the URL and I want the search to start on page load and output a page with the search results. For now (due to simplicity), my model is simply parsing an XML file of a small sample of MARC records. Oh, to top it off...I'm brand new to programming in java and using netbeans. I have no clue about what javabeans are, or any of the more advance techniques. So, with that background explanation here's my question.
I have created a java class (main method) that parses my xml and retrieves the results correctly. I have an index.jsp with my html in it. Within the index.jsp, I have no problem using get to get the title information from my URL. But I have no clue how I would get those parameters to a servlet that contains my java code. If I manage to get the search string to the servlet, then I have no idea how to send that data back to the index.jsp to display in the browser.
So far every example I've seen has assumed you're getting form data, but I need parameters found, processed and returned on page load...IOW, with no user input.
Thanks for any advice.
Ceci
Just put the necessary preprocessing code in doGet() method of the servlet:
String foo = request.getParameter("foo");
// ...
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
And call the URL of the servlet instead of the JSP one e.g. http://example.com/page?foo=bar instead of http://example.com/page.jsp?foo=bar in combination with a servlet mapping on an URL pattern of /page/*.
You can get the url parameter in servlet using request.getParameter("paramName");
and you can pass the attribute to page from servlet using forwarding request from servlet to jsp.
See Also
Servlet wiki page
Bear in mind that your JSP page will compile internally to a servlet. So you can retrieve the string and print it back in the same JSP. For instance assuming you get the string in a parameter called param you would have something like this in your JSP:
<%
String param = request.getParameter( "param" );
out.println( "String passed in was " + param );
%>
One last thing, you mentioned the main method -- that only gets executed for stand alone applications -- whereas you're talking web/JSP :O
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
1) In my servlet program, I have a statement which will be printed using the code as follows,
out.println("<b>This is servlet output</b>");
Instead of getting printed in bold, it just gets printed with the tag in the broswer itself.
How to rectify the same?
2) Also, in the servlet page after the submission of a jsp form, I want to add the below HTML tag inside the java code of the servlet program.
Go to JSP form
How to achieve the same? Please advise.
1) The browser is interpreting your output like text, try adding
response.setContentType("text/html");
This line tells the browser that you're sending HTML and that it should be interpreted that way.
2) The same as bold text
out.println("Go to JSP form");
On a related note, I'd suggest that none of your Servlet class directly write HTML content to the response page. Servlet are made to handle forms, and aren't easy to use when it comes to write HTML responses.
Once thing you can try is to write the response in a JSP page, then forwarding the request to the JSP so it can handle user output.
Here is a sample:
1) servet_output.jsp
<b>My bold test</b>
Go to JSP form
2) Your servlet redirects to the JSP page:
request.getRequestDispatcher("servlet_output.jsp").forward(request, response);
This way, your servlet handles the request, and the JSP takes care of writing the response to the browser.
Don't use servlets for html.jsp is the right place to use that.
just use
request.getRequestDispatcher("name.jsp").forward(request, response);
and write html code there.