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.
rameenvaswaniAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ksivananthCommented:
heloow world would be right choice to start with!

http://java.sun.com/products/jsp/html/jspbasics.fm2.html
0
Bart CremersJava ArchitectCommented:
0
rameenvaswaniAuthor 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.
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

ksivananthCommented:
<!-- 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>
0
rameenvaswaniAuthor 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.
0
ksivananthCommented:
>>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!
0
cmalakarCommented:
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;
}
%>
0
ysnkyCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kunalthakurCommented:
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;
}    %>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.