render xhtml file from servlet - java

I wonder if there is a way to load, interpret and render a .xhtml file from a servlet.
What I want is to have a xhtml file witch contain for example a custom row within a list, to be loaded from a servlet or a bean, to be interpreted, and to obtain the html result.

Sounds to me like you're not in need of Servlets or JSF, but rather JSP (should look familiar, coming from PHP).
Notice that people here will tumble over themselves telling you to not put the logic into the page (and for good reason), but I think with your background, you'll be helped by starting from a JSP only approach. Than start moving the logic to a servlet. You can use a number of techniques to communicate the data between the servlet and the jsp. I like to use
request.setAttribute("someString", valueObject);
In your jsp you than can use:
<%= request.getAttribute("someString") %>
Calling your jsp from the servlet is done via
RequestDispatcher rd = req.getRequestDispatcher("/path.to.your.jsp");
If you really need a .xhtml file (your original question), you can use this last line as well, but it'll be real xhtml, like it would be served from a plain webserver (!= appserver)

Related

Does JSP provide the dynamic behaviour to a web-page?

Although I know JSP (basic understanding), I have some doubts on the JSP technology.
Consider the below simple jsp:
<html>
<head>
<title> This is demo</title>
</head>
<body>
<h1> The current time is <%=new java.util.Date()%> </h1>
</body>
</html>
With respect to this, I have some doubts (which I have been keeping at the back of my mind):
1) The basic text of this jsp is same, is it dynamic page because it has JAVA code in it?
2) When a user accesses this jsp page, does the container first execute the java code and replaces the output of java code inside the page?
3) What makes this a jsp page? Do mixing of html and java code make it a jsp?
4) Can the java code (within <% %> ) live independent of html? or they are coupled (the java code has to be present in html page).
They might be basic questions, can anyone help me in understanding them?
Yes it is dynamic because it is computed (even partially) at run time - here the java scriptlet is the dynamic part
Not exactly. JSP is not a template engine. JSP pages are fully translated to java source and then compiled to class files by the servlet container. Then those classes are executed at run time.
the extension .JSP makes it a JSP file. Then it must respect the JSP syntax to be correctly processed by a servlet container.
Reverse the question. Java code normally lies in java source files. A Java class implementing the HttpServlet interface can be directly map to an URL by the servlet container. And Java code can lie in a scriptlet in a JSP
But as you were said in comment, you really should read documentation on that before asking questions here.
1) it's dynamic if it contains any JSP elements, like code snippets, JSP tags etc. If it contains only HTML, then it's pretty static, although if it is processed as a JSP, then the constant response is computed dynamically on each call (safe Caching).
2) yeah much like that. Actually the static text of the JSP goes into out.write() Statements in a Java class; the whole JSP is transpiled to a Java class.
3) funny question. It's all a question of Interpretation. If you name it .jsp or have your web container process it as JSP in some other ways (depends), then you may call it a JSP.
4) this question is not quite clear. The snippets are executed after the static text has been output up to this point.

JSP with conditional checks is static or dynamic

If I am going to include JSP if condition tag or any Conditional check tags will the JSP is static of dynamic. If I have a dispatcher configured for my webserver. Will the JSPs will run on each request or will the HTMLs that are rendered with the JSPs will be cached?
I think that the conditional tags are what make a page dynamic, because if it only is plain text will display the same thing over and over, but adding conditional statements make the page react to the user.
The JSP will be rendered on each request, the cashes are as simple HTML file in your browser's cache.

How to use JSP as a template without serving it as a webpage

Per my understanding JSP is something to serve to the client. But is it possible to use JSP simply as a template to dynamically assemble an html page, which I then serve to the client? What I mean is this
A servlet receives the call from the user
After some computation, my servlet calls the JSP to assemble the html page dynamically
The servlet gets or converts the JSP "result" (the resulting html page) to a String
The servlet can now do whatever it wants with that String. It can return it as an html webpage or it can store it in a database, or whatever. After all, the string here is a proper html page/text.
For comparison, Python has Jinja2, which does exactly what I just explained. The closest thing to Jinja2 in Java seems to be JSP.
I need a template to assemble html pages dynamically. If I can use the JSP as above then that will solve my problem in Java. Notice that I don't care for JSP per se. I just need a template similar to Jinja2 (if I could use Jinja2 in Java on App-Engine that would be ideal). Also I am very new to JSP. So if you have an answer, please format it as an example; that would be truly helpful.
I am migrating from Python App-Engine to Java App-Engine for business reasons.
This is possible, but you'll need to jump through quite a few hoops, the details of which are dependent on the specific container - in this case appengine.
A quick summary:
create a fake httpservletresponse, wrapping an output stream you access after rendering. You cannot use a httpservletresponsewrapper, even though the spec permits it this environment won't
store all request attributes in a map, you'll restore these afterwards in case they've been mutated
use requestdispatcher.include, passing in the real request and your synthetic response
restore request attributes
read string from the outputstream
Be particularly careful of side effects to your request/response, for example the constraints around only calling one of getwriter or getoutputstream, as well as finalizing the request (setting status or content length)
Or just use one of velocity, handlebars, freemarker or the various other Java templating languages. They'll all be much more straightforward.

Asynchronous JSP Tag

Is there a JSP tag that allows that part of the page to process asynchronously? If not is there a way to create one?
Something like the following:
<Util:Asynchronous>
<%
DataSource source = ...
%>
</Util:Asynchronous>
... rest of page
I don't want to have to wait for the data source to finish before processing the rest of the page.
Thanks.
You need to use AJAX. Also it is bad coding practice to put your data access commands in your JSP pages. Very susceptible to hacks, not to mention performance hits.
The servlet container must wait that you are page is entirely rendered before sending it back to the client. Thus your requirement makes no sense for me.
If a part of the page takes too much time to process, you could use Ajax to load a portion of your page. There is already pages at StackOverflow about Ajax.

parameters stability in jsp page

I am working on JSP-servlet application and now I am programming a page which edit information about registrars.
The senario is that there's an ArrayList I send form servlet to JSP page when I load the page. The ArrayList contains information about groups the registrar belongs, the ArrayList is resulted after making multiple SQL statements. When user try to edit some fields and make one required field empty and submit the form, the servlet makes validation and return error to edit page.
The problem I face is that all the groups I sent in the first time fly in the sky. So I have to make connection to DB again and make multiple queries to get the groups again and send it back to JSP page.
Is there's another simple way to make arrayList stable in JSP page ?
EDIT
Here's the code which I make the scope of the ArrayList in the session.
<c:set var="userGroups" value="${userGroups}" scope="session"></c:set>
Either store it in the session or just live with it. I really don't see any issues with that. If the concrete problem is that you have to copypaste the same code again or that the whole code is ugly to have in a Servlet class, then just refactor/hide that into an useable DAO class which you import/call/reuse in the Servlet the usual Java way.
Update: as per your update, this doesn't make sense. You just need to change your servlet code from
request.setAttribute("userGroups", userGroups);
to
request.getSession().setAttribute("userGroups", userGroups);
You don't need <c:set> for this.

Categories

Resources