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

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

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.

Responding with JSON Array in a JSP page

So I'm working on an old application and I'm forced to use JSP at the moment, which I'm extremely unfamiliar with (and unfamiliar with most aspects of web development tbh).
I'm generating a JSON Array using JSONSimple in a Java class method, and then invoking that method from a JSP on a GET request to get the JSON. Sounds simple enough.
I was able to get my JSON when accessing the URL in my browser. Now I'm trying to access my JSP page from another application, and that's where I'm having difficulty. The content type of my "JSON" was text/html, and not JSON, so I tried setting the content type to JSON in my JSP, and now nothing is showing up in browser, and my Spring application is giving me this error when I try to get JSON from the URL: Could not read document: No content to map due to end-of-input at....
Here's my JSP:
<%# page import="com.company.Someclass" %>
<% if(request.getMethod().equals("GET")){
response.setContentType("application/json");
Someclass.getJSONArray();
}
else if(request.getMethod().equals("POST")){
//todo
}
%>
Any help would be greatly appreciated as I'm pretty lost at the moment. Thanks!
Fixed this myself. I'm able to get JSON in the browser, but having trouble reading it in my Spring application. but thats another battle. Here's my solution:
<%# page import="com.company.Someclass" %>
<% String ret = "";
if(request.getMethod().equals("GET")){
response.setContentType("application/json");
Someclass.getJSONArray().toJSONString();
}
else if(request.getMethod().equals("POST")){
//todo
}
%>
<%= ret %>
the toJSONString() is a method of json-simple's JSONArray class

JSP and HTML parser for 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 ! ;)

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

HTML + Javascript Renderer that outputs HTML or plaintext?

If I use:
String plain = Html.fromHtml(html).toString;
to render simple 'html' that contains:
<!doctype html>
<html>
<head><meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Google</title>
</head>
<body>any plain vanila HTML goes here
</body>
All is nice and dandy.
But what if that page contains tons of Javascript code that is nicely rendered by all web browsers but isn't available to me?
Is there a renderer that takes care of the Javascript as well, to output HTML or plaintext, that isn't necessarily going to a visual display?
(I know about WebView but my understanding that I can't really access its output. Or can I?)
Is there a renderer that takes care of the Javascript as well, to output HTML or plaintext, that isn't necessarily going to a visual display?
WebView or bust.
(I know about WebView but my understanding that I can't really access its output. Or can I?)
Create a Java object to receive your output
Add that Java object to the WebView via addJavascriptInterface()
Use loadUrl("javascript:...") on the WebView to invoke a hunk of Javascript that gathers your information and calls a method on your Java object

Categories