• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Different Output using setContentType

 
Gaurav Chikara
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@ page language="java" import = "javax.servlet.http.*,javax.servlet.jsp.*" %>
<%= "Joe said >" %>

<%="hi!"%>
<% String joes_statement = "hi!"; %>
<%= "Joe said \"" + joes_statement + "\"." %>
<%= "Joe said " +'"'+joes_statement +'"'+"." %>
<%= "\"aa\\" %>
<%= "\''aa\\" %>
<%--<%= '\"aa' %>--%>
<% for(int i=0;i<10;i++)%>
<%= "java" %>
<%--<%= "<% a=1+1; %>" %>--%>
<%--<%= \"name\"" %>--%>
<%! boolean volatil; %>
<%--<%!long strictfp=8;%>--%>
<% session=pageContext.getSession();
session.setAttribute("TestValue","Hello Doctor");
String s=(String)session.getAttribute("TestValue");
out.println("" + s); %>
<%
String s2=request.getParameter("param");
out.println(s2);%>
%>
<%! int x=1;%>
<% x = x + 1999; %>
<%= x %>
gives the output
Joe said > hi! Joe said "hi!". Joe said "hi!". "aa\ ''aa\ java Hello Doctor null %> 2000
but if i use
<% response.setContentType("");%>
wherever in code the output is
Joe said >

hi!
Joe said "hi!".
Joe said "hi!".
"aa\
''aa\
java

Hello Doctor
null
%>

2000
why is it so?
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simple answer is that the setContentType() method changes the way, the JSP handles white space. Section JSP.2.3.7 of the Spec deals with the white space. Section JSP.3.3 deals with setContentType method and this is what it says:

Whitespace is notoriously tricky for JSP pages in JSP syntax, but much more manageable for JSP pages in XML syntax.

Honestly, I din't understant that in the way it is said. If I had more time, I would try some examples and attempt to understand. Also, Selctio JSP.2.3.7 gives some examples you might want to take a look.
Sorry if this doesn't help much....
regds.
- madhav
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic