We help IT Professionals succeed at work.

How to use Java within JSP

rameenvaswani
on
Just wanted to know, how can I use Java code within a JSP file?

A code sample would be appreciated along with how to import packages too in the JSP and any prerequisites for doing it.

Thanks.
Comment
Watch Question

heloow world would be right choice to start with!

http://java.sun.com/products/jsp/html/jspbasics.fm2.html

Author

Commented:
I didn't ask for links, I am capable of searching..

I want some code examples right here, I don't have time to root through pages of resources.
<!-- Import here-->
<%@ page import="hello.NameHandler" %>
<!--define scope of the bean vars-->
<jsp:useBean id="mybean" scope="page"
          class="hello.NameHandler" />
<jsp:setProperty name="mybean" property="*" />

<html>
<head><title>Hello, User</title></head>
<body bgcolor="#ffffff" background="background.gif">

<%@ include file="dukebanner.html" %>

<table border="0" width="700">
<tr>
<td width="150"> &nbsp; </td>
<td width="550">
<h1>My name is Duke. What's yours?</h1>
</td>
</tr>
<tr>
<td width="150" &nbsp; </td>
<td width="550">
<form method="get">
<input type="text" name="username" size="25">
<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</td>
</tr>
</form>
</table>

<!--JAVA Code...-->
<%
    if ( request.getParameter("username") != null ) {
%>

<%@ include file="response.jsp" %>

<%
    }
%>

</body>
</html>

Author

Commented:
@ksivananth

I believe you just copied and pasted the content of a link you posted. I don't understand what on Earth is going on that code. All I need is a basic example that is explained.
>>I believe you just copied and pasted the content of a link you posted

Yes but with my comments!

>>I don't understand what on Earth is going on that code. All I need is a basic example that is explained.

Thats a very basic example, see my comments with in the code and execute the file and see the outputs!

Commented:
Java code inside the jsp can be written using scriplets..

There are basically three types of scriptes..

1) Expressions: <%= expression %>

ex: <%= someVariable%> //this will display the value in the jsp.

2) Scriptlets: <% code %>

ex.. <% while(true)
{....} %>

3) Declarations: <%! code %>

ex:-- //these declarations goes into class level...

<%!
int cnt=0;
private int getCount(){
//increment cnt and return the value
cnt++;
return cnt;
}
%>
Commented:
here it is;
your class
----------------------
package org.ysnky;
 
public class Test {
	
	public String hello() {
		
		return "hello jsp";
	}
}
 
 
 
your jsp
--------------------------
<%@ page import="org.ysnky.Test"%>
<body>    
<% 
Test t = new Test();
%>
<p><%=t.hello()%></p>
</body>

Open in new window

You just need to place your java code within these in a jsp  <%   %>
<% marks the begining of the snippet and \
%> marks the end.

Be careful to use only java statements within these and not any html/jsp stuff. if you want html and jsp stuff also then first close %> and put your jsp content and then again ope <% for java code.

eg.
<% while(true)
{
    %>
<TABLE>
  <TR>  </TR>
  <TD>   </TD>
</TABLE>

<% int a=a+1;
}    %>

Explore More ContentExplore courses, solutions, and other research materials related to this topic.