Contact Us
  
 
 
    SearchSearch   RegisterRegister   Log inLog in 

view problem with special character in JSP

view problem with special character in JSP

 
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans
View previous topic :: View next topic  
Author Message
lungten



Joined: 08 Dec 2008
Posts: 2

PostPosted: Fri Dec 26, 2008 9:57 am    Post subject: view problem with special character in JSP Reply with quote

Hi

In jsp page, When single ' quot and double " quot comes from database, it (JSP) throw an error. Not on single quot or double quot, if < or > comes same error come and stop executing of JSP. What is the solution to error this error on JSP page.
Back to top
View user's profile Send private message
seniormember
Site Admin


Joined: 20 Jul 2008
Posts: 77

PostPosted: Fri Dec 26, 2008 3:15 pm    Post subject: Reply with quote

In java, String variable content should be inside of double quot.

e.g

String abc= "this is java string";

but if you do like

String abc="this"s java string";
it will not work and give an error.

Red red start and end block is completed, not in blue it is uncompleted. Which need to be closed.


"---------"

One way for completing it as puting \

String abc=\"this"s is java string\";
work fine
Back to top
View user's profile Send private message
mehra2007



Joined: 13 May 2007
Posts: 4

PostPosted: Sat Dec 27, 2008 7:50 am    Post subject: Reply with quote

This is fine with single string but if you want to escape special characters from whole string. You have to remove special and convert into html format.

In Jsp, string with special characters terminated by unclosed literal.

I am giving a method which can remove some special characters from string. It can modify for more special character

Method of escaping

1. "
2. '
3. ,
4. \
5. <
6. >
7. &


Code:
public String convSpecialChar(String str)
{
   str=str.replaceAll("\"", "&quot;");
   str=str.replaceAll("\'", "&#39;");
   str=str.replaceAll(",", "&#44;");
   str=str.replaceAll("\\\\", "&#092;");
   str=str.replaceAll("\\<", "&lt;");
   str=str.replaceAll("\\>", "&gt;");
   str=str.replaceAll("&", "&amp;");
   return str;
}



Code:
<div><%=convSpecialChar("StringObjectFromDatabase")%></div>
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans All times are GMT
Page 1 of 1

 
Jump to:  
  Quick Post a Reply Topic
     
  Name (required)  
     
  Email Address (will not be published) (required)  
     
  Subject of topic (required)  
     
   
     
   
     
  Advertisement  
     
 
     


  More Links  
 
JSP Introduction
JSP Setup
Simple JSP Example
JSP Variable
Array Declaration in JSP
JSP Forms and User Input
Form Validation
JSP Cookies
JSP Session Object
Loop, content collection, iterator, conditional check in JSP
JSP Application Object
JSP Include
JSP Response Object
JSP Request Object
JSP Error Handling
JSP File Handling Object
JSP Action Tag
JSP Implicit Objects
JSP Declaration
JSP Directive
JDBC Introduction
JDBC Driver
JDBC Connection
JDBC Prepared/Statement
JDBC ResultSet
JDBC Close Connection object
JDBC Insert
JDBC Display Records
JDBC Update
JDBC Delete
JDBC Sorting
JDBC Query
JDBC Exception
JDBC Add Batch
JDBC Transaction/RollBack