PREVIOUS
(Declaration of variable)
2. j2.jsp
Function using jsp :
4. j4.jsp
PREVIOUS
1. To access component that resides on server
2. To produce dynamic contents
How jsp work (same as servlet):
1.
Request to jsp
page from client/browser
2.
Request send to
web server
3.
Request is
executed in jsp engine (web server)
4.
jsp engine generates
a servlet
5.
compiling and
executing the servlet
6.
Resulting is send
to client(browser)
·
Servlet is a java
component. Using jsp we can access any java component from server (may be a
servlet)
JSP Component:
Html(tags)
Scrip lets ( <%!
………%>)
Directives (<%@.........%>
Action( <jsp:
action_name>.........</jsp:action_name>)
Objectàout, Exception, response, request, session,
application
Directives (Information
about the jsp page):
1.page
2. Include
3. taglib
Sytax:
<%@directive_name
attribute=value attribute=value%>
scriplets:
<%.................%>
Declaration:
<%!datatype
var1,var2……..%>
Eg:
<%! String x;
int x,y;
double a,b;
%>
Function
declaration:
<%! access_specifier return
type function(argument)
{
function definition;
}
%>
<%!public String show()
{
return (“hello”);
}
%>
Expression:
Syntax:
<%=expression(variable/function
call)%>
Eg:
<% =x%>
<% =show( )%>
First create a
jsp program in:
c:\program files\apache tomcat
4.0\webapps\examples\jsp\ani\first.jsp
note: here jsp is folder
name(we can create folder in the jsp but not in the servlet)
Eg:first.jsp
<html>
<body>
Message using html
<br>
<%
out.println(“Message from
jsp”);
%>
</body>
</html>
Step to
execute:
1. c:\program files\apache tomcat 4.0\bin>startup
(start the apache server)
2. Open
internet explorer
http//localhost: 8080/examples/jsp/first.jsp
note:
(here no need to compile the
program)
Programs:
1.
j1.jsp
<%
out.println("Hello"); %>
<%
out.println("<br>"); %>
<%
out.println("Hai"); %>
<%
out.println("<br>"); %>
<%
out.println("Good"); %>
<%!
String x;%>
<%
x=”Anu”;
out.println(“Hello world : ! “+x);
%>
3. j3.jsp
<%! String x;
String fun(String s)
{
return s;
}
%>
<%
x=fun("Welcome");
out.println("Hai :" +x);
%>
Declaring class:
<%!
Public class first
{
int no;
String nam;
Public void get(int sno,String snam)
{
no=sno;
nam=snam;
}
public String dis()
{
return(“No :“+no+
“\n”+ “Name” +nam);
}
}
%>
<%first s=new first();
s.get(100,”amar”);
out.println(s.dis());
%>
1. Getting
data from html to jsp:
Html: (first.html )
<html>
<body>
<form
action=”http://localhost:8080/examples/jsp/j5.jsp” method=”get” name=”f1”>
user
name<input type=”text” name=”t1”>
password<input
type=”password” name=”t2”>
<input
type=”submit” name=”submit” value=”submit”>
</form>
</body>
</html>
(jsp program for above
html)
j5.jsp:
<%!String
nam;
String pwd;
%>
<%
nam=request.getParameter(“t1”);
pwd=request.getParameter(“t2”);
out.println(“Name :” +nam+ “<br>”);
out.println(“Password :” +pwd);
%>
2. Getting values from html to jsp:
Html : (second.html)
<html>
<body>
<form
action="http://localhost:8080/examples/jsp/ani/li.jsp"
method="get"
name="f1">
Name<input
type="text" name="name"><br>
Mark1<input
type="text" name="ma1"><br>
Mark2<input
type="text" name="ma2"><br>
Mark3<input
type="text" name="ma3"><br>
<input
type="submit" name="s" value="submit">
</form>
</body>
</html>
(jsp program for above html program:)
j6.jsp
<%! String nam;
int m1,m2,m3,tot;
float avg;
%>
<%
m1=Integer.parseInt(request.getParameter("ma1"));
m2=Integer.parseInt(request.getParameter("ma2"));
m3=Integer.parseInt(request.getParameter("ma3"));
nam=request.getParameter("name");
tot=m1+m2+m3;
avg=tot/3.0f;
out.println("Your
name is :"
+nam+"<br>");
out.println("Your
total is :"
+tot+"<br>");
out.println("Your
Average is :"
+avg+"<br>");
%>
PREVIOUS
No comments:
Post a Comment