Link to home
Start Free TrialLog in
Avatar of rameenvaswani
rameenvaswani

asked on

How to use Java within JSP

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.
Avatar of ksivananth
ksivananth
Flag of United States of America image

heloow world would be right choice to start with!

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

ASKER

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>
@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!
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;
}
%>
ASKER CERTIFIED SOLUTION
Avatar of ysnky
ysnky
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
}    %>