JSP and HTML parser for JAVA - java

I have been using Jsoup for parsing my HTML files and so far it does a great job. However, it's not able to parse any server tags ( <% ... %> ). I decided to extend it but I cannot find an easy way to extend its Parser and all those private/package level classes (i.e. TreeBuilder, TransitionState ... etc)...
So I started looking at Jericho as it claims it can parse server tags - however, its documentation is so poor that I cannot even get started easily. And seems like its API is not as friendly as what Jsoup provides - it's not that straight forward to extract some nodes and move it around ...
I wonder if anyone has the similar situation before and how you get it solved? In short, I just want to parse JSP files in Java. (Well .. please don't ask me to implement one by myself ;p )

Lastly I get a workaround: put server code block in a HTML comment block so that 1) server code can get executed correctly; 2) Jsoup can process the whole block as a HTML comment node without touching anything inside.
e.g.
<!--
<%# page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%# page import="com.systemcrossed.groupbuystart.webapp.display.DisplayHelper" %>
<%# page import="com.systemcrossed.groupbuystart.webapp.util.JsonUtil" %>
<%# page import="org.apache.commons.lang.StringEscapeUtils" %>
<%# include file="/_sys/pages/public/incl/jspCommon.jsp" %>
-->
<!--<%
// Java code here
%>-->
<html>
<head>
... html stuff
It works well for me now! Hope ppl who got the same problem could get some help ! ;)

Related

How to print JSON response from JAVA method to HTML Table?

I have a jsp code, where I fetch some JSON data from JAVA Class file. [Basically openfire users]
Now I get the data successfully, but I want to show this data in HTML table format.
How do I Do this ?
My JSP Code :
<%# page language="java" import="prov.*, java.util.*, java.io.*,java.text.*" contentType="text/html"%>
<%# page errorPage="error.jsp" %>
<%
Openfire tc = new Openfire();
tc.getUsers("192.168.50.218","epvFjHq5RHA614C7");
out.println("Data Is As Below : " + tc.getUsers("192.168.50.218","epvFjHq5RHA614C7"));
%>
And I get Response from the JAVA Class method like this :
[{"username":"abcd","name":"","properties":null},{"username":"admin","email":"admin#example.com","name":"Administrator","properties":null},{"username":"bizdd456d454mnc","email":"bizMNC#bizrtc.com","name":"bidzMNC","properties":null},{"username":"bizddd454mnc","email":"bizMNC#bizrtc.com","name":"bidzMNC","properties":null},{"username":"bizmnc","email":"admin#example.com","name":"511515151515151","properties":{"property":[{"#key":"console.order","#value":"session-summary=1"},{"#key":"console.rows_per_page","#value":"user-summary=8"}]}},{"username":"dhaval","email":"dhaval#bizrtc.com","name":"dhaval","properties":null},{"username":"keyur","email":"keyur#bizrtc.com","name":"keyur","properties":null},{"username":"minz","email":"bizMNC#bizrtc.com","name":"bidzMNC","properties":null},{"username":"patel","email":"rau#example.com","name":"patelbhai","properties":{"property":[{"#key":"console.order","#value":"session-summary=1"},{"#key":"console.rows_per_page","#value":"user-summary=8"}]}},{"username":"rajan","email":"rajan#bizrtc.com","name":"rajan","properties":null},{"username":"+username+","email":"+email+","name":"+name+","properties":null}]
As I am very new to JAVA and JSP I don't know how to parse this data to HTML Table.
So Please help.
You can see here how to do it. You can populate it in Javasript or jQuery, but it is better to use JSTL and not just call java code inside JSPs.
I would suggest you use mustache als template engine.
It allows you to use a HTML fragment as template (store it as resource) where double curly brackets (hence the name Mustache) denote the insertion points.
The full documentation of the Mustache syntax is here and a Java example here. Let us know how it is going.

Struts2 - pass enye character from jsp to struts 2 action class properly

I have a webapp built using Struts2, with Tomcat 7 as application server and MySQL database as backend. Now i pass some string from my jsp file using jquery ajax but this string contains special characters (ñ - enye). When i alert it before passing to the class via jquery ajax, it displays fine but when i receive it in the java class, it displays like ñ . I need the string to be as exact as possible as i am querying the mysql database with an entry similar to the string.
All jsp pages have <%#page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> and <meta http-equiv="content-type" content="text/html; charset=utf-8"/> in head as well as the jquery contentType contentType: 'application/json; charset=utf-8'.
What could be the possible solution to this? Thanks for your reply. Here are the screenshots by the way. Thanks for your help :)
Luckily found the answer within StackOverflow. Thanks guys, you're the best :)
How to determine if a String contains invalid encoded characters

J2EE - How to remove empty space,blank lines from server response

Is there a way to remove blank lines from server response? I have tried out:
<init-param>
<param-name>trimSpaces</param-name>
<param-value>true</param-value>
</init-param>
and
<%# page trimDirectiveWhitespaces="true"%>
which didn't solve the issue properly since in init param method it removed even space between words, 2nd method didn't work either since it needs java servlet version to be 2.5.
Your advice will be very helpful ..
you should create a filter-servlet that remove blank lines and chained the response to your servlet.
when the server convert the JSP page to servlet it add implicitly blank lines (\n).
add out.clear() befor writing data to clear out object.
Add the following directives to your page and the lines should disappear:
<%# page contentType="text/xml" %>
<%#page pageEncoding="UTF-8"%>
<%# page trimDirectiveWhitespaces="true"%>
I think, you should use an IDE like eclipse. Where you can just select the whole code and format it and follow all java syntax guidelines by just right clicking and seletcing a few options.
Please use good IDE's all of them will help you follow good guidelines and make your work simpler and easier to read and understand

How To Stop Tomcat/Java Wrapping My Output

I've got a JSP page that I want to return a fragment of HTML. The trouble is that whenever I request the JSP, something is attempting to make the HTML more valid by wrapping <html> tags around it. I don't want it to do this though as it will be used in a variety of other places.
For an example, the following JSP:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script src="${applicationConfig.javascriptUrl}update.js" language="javascript" type="text/javascript"></script>
<p>Wibble</p>
Will result in the following HTML:
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><script src="http://fisher.mycompany.com:8080/my-app/includes/js/update.js" language="javascript" type="text/javascript"></script>
<p>Wibble</p></html>
I really don't want those <html> & <head> tags there and would like to get rid of them but have no idea where this is happening to turn it off. Does anyone have any clues?
* Edit *
To give a little more information on what I am trying to achieve. This JSP will check a variety of things and form a piece of HTML. This HTML can then be included into other applications via a web service call.
Servlets can return any content type including javascript and images, not just HTML. Tomcat should not wrap jsps in extraneous tags. I put the snippet you suggested in a jsp, minus the taglib which I don't have set up, and got back exactly the HTML that I put in.
Can you tell us more about your environment? Are you using tomcat? Are you using some kind of framework?
Servlets are HTML factories. They expect to send a valid HTML page down to a browser to be rendered. You can't "get rid of it" without breaking the whole model.
Your original concept of sending a snippet that's "used in a variety of other places" is flawed. You sound like to want to set some data that might be used in other places - that's valid - but I don't see how wrapping it in markup matters.
Only the JSP should be using the marked up data. JSPs are all about display. I'd rethink what you're doing and attack how you want to share the data, not the markup.
One approach it might work,
Create HTML files as you required valid HTML,
and use servlet to returns response, servlet should read HMTL File and return
its contents as String, like XML respones from servlet
hopes thats helps

"someVariable cannot be resolved" error indicated in Eclipse for an included JSP

So I have a main JSP index page, "index.jsp", which has several includes. Something like,
<%# include file="/WEB-INF/views/includes/jstl/include.jsp" %>
<%# include file="/WEB-INF/views/includes/licenses/license.jsp" %>
<%# include file="/WEB-INF/views/includes/generalHtml/header.jsp" %>
<%# include file="/WEB-INF/views/includes/navigation/navbar.jsp" %>
<%# include file="/WEB-INF/views/includes/generalHtml/footer.jsp" %>
The "include.jsp" contains a string "String basePath = request.getContextPath();".
"navbar.jsp" uses "basePath", but Eclipse puts a red X on any line of "navbar.jsp" that uses "basePath" with the message "basePath cannot be resolved".
My webapp works just fine, but wondering what might be causing this, or if it can be resolved? Or maybe there's a way to make Eclipse ignore this? I just want the false positive red X's to go away. I suppose Eclipse doesn't understand that "navbar.jsp" is getting "basePath" from "include.jsp" because it is compiled together with "index.jsp".
Eclipse is an epic failure with regard to JSP/EL validation. Just ignore it or disable all (or parts of) JSP/EL validation in workspace preferences through Validation and/or Web > JSP Files > Validation.

Categories