JSP Example


package Pack;
public class Student implements java.io.Serializable
{
private int studentid;
private String studentname;
public Student(){}
public void setStudentid(int studentid){
this.studentid=studentid;
}
public void setStudentname(String name){
this.studentname=name;
}
public int getStudentid(){
return studentid;
}
public String getStudentname(){
return studentname;
}
}
—————–BEANEG.JSP————————–
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<jsp:useBean id=”Student” class=”Pack.Student”>
<jsp:setProperty name=”Student” property=”studentid” value=”1″ />
<jsp:setProperty name=”Student” property=”studentname” value=”pratik” />
</jsp:useBean>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
</head>
<body>
<h2>Details are</h2>
<table border=”1″>
<tr>
<td>ID :</td>
<td> <jsp:getProperty name=”Student” property=”studentid” /><br></td>
</tr>
<tr>
<td>Name : </td>
<td><jsp:getProperty name=”Student” property=”studentname” /></td>
</tr>
</table>

</body>
</html>
————————–DISPALY.JSP———————————————–

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<%@page errorPage=”error.html” %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%!
public int div(int num1,int num2)
{
return num1/num2;
}

%>
<%
int num1=Integer.parseInt(request.getParameter(“txtnum1”).toString());
int num2=Integer.parseInt(request.getParameter(“txtnum2”).toString());
out.write(“Division is “+div(num1,num2));
%>
</body>
</html>
—————————–DISPLAYLOG.JSP`———————————————
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<%@page import=”java.sql.*”%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
try{
Class.forName(“com.mysql.jdbc.Driver”);
Connection con1=DriverManager.getConnection(“jdbc:mysql://127.0.0.1/Demo”,”root”,””);
String Query=”select * from log”;
Statement st1=con1.createStatement();
ResultSet rs=st1.executeQuery(Query);
%>
<table border=”1″>
<tr>
<th>Accesed Time</th>
<th>IP address</th>
<th>Hits</th></tr>
<%
while(rs.next()){
%>
<tr>
<td> <% out.write(rs.getString(1)); %> </td>
<td> <% out.write(rs.getString(2)); %> </td>
<td> <% out.write(rs.getString(3)); %> </td>
</tr>
<% }
%>
</table>
<%
con1.close();
}
catch(Exception ex){
out.write(ex.toString());
}
%>
</body>
</html>

———————————-PRACT14C.jsp——————————————–
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<%@page import=”java.io.*” %>
<%@page import=”java.sql.*” %>
<!DOCTYPE html>
<%
//Session creation time
Date visittime=new Date(session.getLastAccessedTime());
Integer visit=new Integer(0);
String visitkey=new String(“visit”);

if(session.isNew()){
session.setAttribute(visitkey,visit);
}
visit=(Integer)session.getAttribute(visitkey);
visit++;
session.setAttribute(visitkey,visit);
%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
</head>
<body>
<center>
<h1>Session Tracking</h1>
</center>
<table border=”1″ align=”center”>
<tr bgcolor=”#949494″>
<th>Session info</th>
<th>Value</th>
</tr>

<tr>
<td>Visited Time</td>
<td><% out.print(visittime); %></td>
</tr>
<tr>
<td>Number of visits</td>
<td><% out.print(visit); %></td>
</tr>
<tr>
<td>IP address</td>
<td><% out.print(request.getLocalAddr()); %></td>
</tr>
</table>
<%
try{
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://127.0.0.1/Demo”,”root”,””);
String Query=”insert into log values(‘”+visittime+”‘,'”+request.getLocalAddr()+”‘,”+ visit +”,'”+session.getId()+”‘)”;
Statement st=con.createStatement();
st.execute(Query);
con.close();

}
catch(Exception ex){
}
%>
<%
try{
Class.forName(“com.mysql.jdbc.Driver”);
Connection con1=DriverManager.getConnection(“jdbc:mysql://127.0.0.1/Demo”,”root”,””);
String Query=”select * from log”;
Statement st1=con1.createStatement();
ResultSet rs=st1.executeQuery(Query);
%>
<center>
<table border=”1″>
<tr>
<th>User ID</th>
<th>Accesed Time</th>
<th>IP address</th>
<th>Hits</th></tr>
<%
while(rs.next()){
%>
<tr>
<td> <% out.write(rs.getString(4)); %> </td>
<td> <% out.write(rs.getString(1)); %> </td>
<td> <% out.write(rs.getString(2)); %> </td>
<td> <% out.write(rs.getString(3)); %> </td>
</tr>
<% }
%>
</table>
</center>
<%
con1.close();
}
catch(Exception ex){
out.write(ex.toString());
}
%>
</body>
</html>

————————————–WELCOME.JSP——————————————-
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
</head>
<body>
<h1>Online Shopping</h1>
<table border=”1″>
<tbody>
<tr>
<td>Product 1</td>
<td>Product 2</td>
<td>Product 3</td>
<td>Product 4</td>
<td>Product 5</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</body>
</html>

—————————————–LOGIN.HTML————————————————-
<html>
<head>
<title>TODO supply a title</title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
</head>
<body>
<form name=”login” action=”welcome.jsp”>
<table border=”1″>
<tbody>
<tr>
<td>Username</td>
<td><input type=”text” name=”txtuser” value=”” size=”50″ /></td>
</tr>
<tr>
<td>Password</td>
<td><input type=”password” name=”txtpass” value=”” size=”50″ /></td>
</tr>
<tr>
<td colspan=”2″><input type=”submit” value=”Login” name=”btnlogin” /></td>
</tr>
</tbody>
</table>

</form>
</body>
</html>

—————————————-ERROR.HTML——————————————
<html>
<head>
<title>TODO supply a title</title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
</head>
<body>

Something went wrong

</body>
</html>

————————CALCULATOR.JAVA——————————————————–
<!DOCTYPE html>
<!–
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
–>
<html>
<head>
<title>TODO supply a title</title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
</head>
<body>

<form name=”calc” action=”display.jsp” method=”POST”>

<table border=”1″>
<thead>
<tr>
<th>Number 1</th>
<th><input type=”text” name=”txtnum1″ value=”” size=”150″ /></th>
</tr>
</thead>
<tbody>
<tr>
<td>Number 2</td>
<td><input type=”text” name=”txtnum2″ value=”” size=”150″ /></td>
</tr>
<tr>
<td colspan=”2″>
<input type=”submit” value=”Divide” name=”btndivide” />
</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

————————————————————————–