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.

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.

heloow world would be right choice to start with!
Bart CremersJava ArchitectCommented:
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.
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.

<!-- 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="*" />

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

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

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

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

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


rameenvaswaniAuthor Commented:

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
return cnt;
here it is;
your class
package org.ysnky;
public class Test {
	public String hello() {
		return "hello jsp";
your jsp
<%@ page import="org.ysnky.Test"%>
Test t = new Test();

Open in new window


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
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.

<% while(true)
  <TR>  </TR>
  <TD>   </TD>

<% int a=a+1;
}    %>
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

From novice to tech pro — start learning today.