0

I was trying to write a JSP page. But i have read many times that source code within JSP page is a bad practice. So then I tried to write a different class in the same package and call it within that JSP page.

Here's the JSP code:

<jsp:useBean id="link" scope="application" class = "tms.TestJava" />
<%

TestJava t=new TestJava();
t.test();

%>

and here's the class code:

public class TestJava {

public void test() throws IOException
{
    System.out.println("sdds");
}
}

I have imported the class into JSP page.

Now the problem is that when I use System.out.println in the class (test method), it gets printed onto the console and I want it to print it to the JSP page. How can I achieve this? Is there a seperate method? Do I have to make the class a servlet?

Thanks!

nj-ath
  • 3,028
  • 2
  • 25
  • 41
  • 1
    That's still Java source in a JSP page. – Dave Newton Apr 29 '14 at 14:48
  • Not the business logic – nj-ath Apr 29 '14 at 14:49
  • The point was that you shouldn't have any Java source in your JSPs. There's no reason to do it. Use a custom tag, either JSP- or Java-based, or use JSTL's `fn` to call functions. Which is better depends on the specifics. Both are searchable and have examples on how to get page output. Obviously `System.out` isn't the same as the JSP's `PrintWriter`, though. – Dave Newton Apr 29 '14 at 14:56

2 Answers2

2

Try using a tag library: JSTL

Specifically:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:out> Tag Example</title>
</head>
<body>
<c:out value="${'<tag> , &'}"/>
</body>
</html>

Or better yet, since you are using JavaBeans already, refactor so that you aren't using System.out() anymore. The idea is that you want to display properties of your bean in your page. Consider: JavaBeans So Do something like this:

Java

public class Course{
   private String code = "Math";
   public String getCode(){
      return code;
   }
}

Jsp

<jsp:useBean id="course" class="com.javaBeans.Course" />
<jsp:getProperty name="course" property="code"/>

Chiefly, you don't want to just System.out() to a page. The page should be a view of the data of components on the sever which in this case is the bean.

zero298
  • 25,467
  • 10
  • 75
  • 100
1

I think you're going about this the wrong way, but here is a possible solution...

<head>
    <%@ page language="java" import="tms.TestJava"%>
</head>

<body>
    <%=TestJava.getAString()%>
    <%=TestJava.getAString()%>
    <%=TestJava.getAString()%>
    <%=TestJava.getAString()%>
    <%=TestJava.getAString()%>
</body>


public String getAString(){
    return "<li></li>";
}

If your goal is to build dynamic JSPs, you will probably want to look into JSTL as someone above mentioned, look into defining your own tags, etc. I don't think very much new dev is using scriplet code.

Half_Duplex
  • 5,102
  • 5
  • 42
  • 58
  • But here I cannot get it to return more than 1 string! My point is whatever i print onto that function in the class should be printed on the browser.. – nj-ath Apr 29 '14 at 15:10
  • I am not sure I follow you, you can call that `<%=TestJava.getAString()%>` method that returns a String as many times as you want, or any other number of functions you've created to return markup. Also, nothing stops you from doing something like this either.. `<%=System.out.println("Print this in html...")%>` – Half_Duplex Apr 29 '14 at 15:30
  • NOTE: To everyone saying this should be avoided... I agree, but it's also fundamental to understanding how JSP works and that appears to me what is needed here. – Half_Duplex Apr 29 '14 at 15:36
  • Ok, let me explain.. Let's say this is a simple interactive application, so I might need to input some random files and output messages accordingly (in different places) and I would like to do this in a single function, so when I call that function in the scriptlet, it should simply execute and print whatever is required to be printed onto the browser! – nj-ath Apr 29 '14 at 15:41
  • 1
    JSP doesn't work that way, the JSP code in your .jsp file is used once to build the page when the .jsp file is requested by the client. Once the servlet container builds the HTML based on the .jsp code and serves it, that's it, you are no longer able to make those calls. If you're asking how you can incorporate user input into this process, you need to read about servlets and mapping them to the JSP page which will allow you to pass parameters to the soon to be generated jsp page. I think you need to read more about JSP and servlets and also the difference between them and AJAX may be helpful. – Half_Duplex Apr 29 '14 at 15:53