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.
A code sample would be appreciated along with how to import packages too in the JSP and any prerequisites for doing it.
Thanks.
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.
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"> </td>
<td width="550">
<h1>My name is Duke. What's yours?</h1>
</td>
</tr>
<tr>
<td width="150" </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("user name") != null ) {
%>
<%@ include file="response.jsp" %>
<%
}
%>
</body>
</html>
<%@ 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"> </td>
<td width="550">
<h1>My name is Duke. What's yours?</h1>
</td>
</tr>
<tr>
<td width="150" </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("user
%>
<%@ include file="response.jsp" %>
<%
}
%>
</body>
</html>
ASKER
@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. 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!
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;
}
%>
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
} %>
<% 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;
} %>
http://java.sun.com/products/jsp/html/jspbasics.fm2.html