0

I have a GET request to a jsp page. The contents of the JSP page is below:

<%@page language="java" import="java.sql.*,java.util.*" %>
<%@page import="javax.servlet.*, java.util.*,java.util.Date, java.text.*"%>
<jsp:useBean id="workM" class="test.concept" scope="page" />
<jsp:useBean id="userM" class="test.learning" scope="page" />
<jsp:useBean id="checkM" class="test.check" scope="session" />
<%
if (checkM.checkUser()) //For auto loading user's most used search engine. Note: Only if the difference between top two is very big
{
    String user_id = checkM.getUser();
    ResultSet RS6=workM.executeQuery("select top 1 searchEngineUsed,count(*) as number from UserSearchEngineSearch where user_id='"+user_id+"' group by searchEngineUsed order by number desc;");
    while (RS6.next())
    {

        String name = RS6.getString("searchEngineUsed");
        if (name.equals("google"))
        {
            out.print("1");
        }
        else if (name.equals("yahoo"))
        {
            out.print("2");
        }
        else if (name.equals("bing"))
        {

            out.print("3");
        }
        else if (name.equals("baidu"))
        {

            out.print("4");
        }
        else if (name.equals("yandex"))
        {
            out.print("5");
        }
        else if (name.equals("askcom"))
        {
            out.print("6");
        }
        else //by default just display google
        {
            out.print("1");
        }
    }
}
else //user not login
{ 
    out.print("1");
}
%>

However, the content of the GET webpage is contains 5 lines of new line followed by a 4 (which means "baidu" was selected). How do I remove the 5 lines of new line spacing?

f_puras
  • 2,521
  • 4
  • 33
  • 38
Ferrino
  • 53
  • 1
  • 10

3 Answers3

1

If you are using Tomcat you could use

<%@ page trimDirectiveWhitespaces="true" %>

on your page, or define a global setting in your web.xml

<jsp-config>
  <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <trim-directive-whitespaces>true</trim-directive-whitespaces>
  </jsp-property-group>
</jsp-config>
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
0

The <%@page> and <jsp:useBean> directives at the top of your JSP are each separated by a newline. If you actually want to remove them, you need to write these in one line. Yet the extra newlines do not harm, they are not rendered by browsers.

f_puras
  • 2,521
  • 4
  • 33
  • 38
0

Also you can rewrite it on to something like this :

<%@page language="java" import="java.sql.*,java.util.*"
%><%@page import="javax.servlet.*, java.util.*,java.util.Date, java.text.*"
%><jsp:useBean id="workM" class="test.concept" scope="page" 
/><jsp:useBean id="userM" class="test.learning" scope="page" 
/><jsp:useBean id="checkM" class="test.check" scope="session"
/><%
......
%>

It will not add any extra new lines.

richardtz
  • 4,993
  • 2
  • 27
  • 38