Sei sulla pagina 1di 18

MVC application:

------------------------
productform.html:

<html>
<body bgcolor="yellow">
<form action="controller.jsp" method="post">
<table align=center>
<tr>
<th colspan=2>PRODUCT FORM</th>
</tr>
<tr>
<td><b>Product ID:</b></td>
<td><input type="text" name="pid" /></td>
</tr>
<tr>
<td><b>Product Name:</b></td>
<td><input type="text" name="pname" /></td>
</tr>
<tr>
<td><b>Product Price:</b></td>
<td><input type="text" name="price" /></td>
</tr>
<tr>
<td colspan="2" align=center><input
type="submit" value="STORE" /></td>
</tr>
</table>
</form>
</body>
</html>

controller.jsp:
-----------------
<jsp:useBean id="pb" class="org.students.ProductBean" scope="request" />

<jsp:setProperty name="pb" property="pid" value='<


%=Integer.parseInt(request.getParameter("pid")) %>' />
<jsp:setProperty name="pb" property="pname" value='<
%=request.getParameter("pname") %>' />

<jsp:setProperty name="pb" property="price" value='<


%=Double.parseDouble(request.getParameter("price")) %>' />

<jsp:setProperty name="pb" property="storeData" value="hai" />

<jsp:forward page="/view.jsp" />

Model class: (Product.java)

package org.students;
import java.sql.*;

public class Product


{
//vars
private int pid;
private String pname;
private double price;

//setters
public void setPid(int pid){
this.pid = pid;
}
public void setPname(String pname){
this.pname = pname;
}
public void setPrice(double price){
this.price = price;
}

//getters
public int getPid(){
return pid;
}
public String getPname(){
return pname;
}
public double getPrice(){
return price;
}

//data access logic in the form of setter


public void setStoreData(String x)throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ti
ger");

String vsql = "insert into product values(?,?,?)";


PreparedStatement pstmt = con.prepareStatement(vsql);
pstmt.setInt(1,pid);
pstmt.setString(2,pname);
pstmt.setDouble(3,price);

pstmt.executeUpdate();
}

}
/*
before compile this program we must set classpath for ojdbc14.jar
compilation:-
cmd:\>javac -d . Product.java
*/

view.jsp:
------------
<jsp:useBean id="p" class="org.students.ProductBean" scope="request" />

<center>
<h2> Your Data stored successfully with following details.</h2>
<b>
Product id:<jsp:getProperty name="p" property="pid" /><br>
Product Name:<jsp:getProperty name="p" property="pname" /><br>
Product Price:<jsp:getProperty name="p" property="price" /><br>
</b>
</center>
URL-Rewriting Application:
------------------------------------
getProductData.jsp:-

<%@page import="java.sql.*" %>


<%
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jbdc:oracle:thin:@localhost:1521:xe","system","ti
ger");

String vsql = "select * from product";

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(vsql);

%>
<html>
<body bgcolor="yellow">
<table border=1>
<tr>
<th>PID</th>
<th>PNAME</th>
<th>PRICE</th>
<th>REMARKS</th>
</tr>
<%
while(rs.next()){
%>
<tr>
<td><%=rs.getInt(1) %></td>
<td><%=rs.getString(2) %></td>
<td><%=rs.getDouble(3) %></td>
<td><a href='delete.jsp?pid=<
%=rs.getInt(1)%>'>Delete</a></td>
</tr>
<%
}
%>
</table></body></html>
<%
}catch(Exception e){
out.println(e.getMessage());
}
%>

delete.jsp:

<%@page import="java.sql.*" %>


<%
//capturing data from query string
String x = request.getParameter("pid");
int id = Integer.parseInt(x);

try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jbdc:oracle:thin:@localhost:1521:xe","system","ti
ger");

String vsql = "delete from product where pid=" + id;

Statement stmt = con.createStatement();


stmt.executeUpdate(vsql);

con.close();

}catch(Exception e){
out.println(e.getMessage());
}
%>

<jsp:forward page="/getProductData.jsp" />


HTTP Cookie Application:
----------------------------------
empForm.html:
--------------------
<html>
<head>
<title>Employee Form</title>
</head>
<body bgcolor=yellow>
<form action="getEmpDetails.jsp" method="post">
<table align=center>
<tr>
<th colspan=2><h2 align=center>EMPLOYEE
FORM</h2></th>
</tr>
<tr>
<td><b>Employee ID:</b></td>
<td><input type="text" name="eid"/></td>
</tr>
<tr>
<td><b>Employee Name:</b></td>
<td><input type="text" name="empName"/></td>
</tr>
<tr>
<td><b>Employee Desig:</b></td>
<td><input type="text" name="empDesig"/></td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="submit" value="NEXT" />
</td>
</tr>
</table>
</form>
</body>
</html>
getEmpDetails.jsp:
------------------------
<%
//capture the data from empForm.html
String eid = request.getParameter("eid");
String empName = request.getParameter("empName");
String empDesig = request.getParameter("empDesig");

//generate cookie objects related to first form data.


Cookie c1 = new Cookie("ceid",eid);
Cookie c2 = new Cookie("cempName",empName);
Cookie c3 = new Cookie("cempDesig",empDesig);

//add all the above cookies into response.


response.addCookie(c1);
response.addCookie(c2);
response.addCookie(c3);

//generate dynamic second form contains remaining fields


%>
<html>
<head>
<title>Employee Form</title>
</head>
<body bgcolor=yellow>
<form action="storeEmpDetails.jsp" method="post">
<table align=center>
<tr>
<th colspan=2><h2 align=center>EMPLOYEE
FORM</h2></th>
</tr>
<tr>
<td><b>Email:</b></td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td><b>Mobile Number:</b></td>
<td><input type="text" name="mobile"/></td>
</tr>
<tr>
<td><b>Employee Age:</b></td>
<td><input type="text" name="empAge"/></td>
</tr>
<tr>
<td><b>Employee Salary:</b></td>
<td><input type="text" name="empSal"/></td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="submit" value="STORE" />
</td>
</tr>
</table>
</form>
</body>
</html>

storeEmpDetails.jsp:
--------------------------
<%@page import="java.sql.*" %>
<%
//capture the data from second form
String email = request.getParameter("email");
String mobile = request.getParameter("mobile");
String empAge = request.getParameter("empAge");
String empSal = request.getParameter("empSal");

//declare some local variables to store cookie information


String empId = null,empName = null, empDesig = null;

//retrieve cookies from the browser's cache memory


Cookie carr[] = request.getCookies();

//Iterate over Cookie array to retrieve cookie data


for(int i=0;i<carr.length;i++){
if( carr[i].getName().equals("ceid") )
empId = carr[i].getValue();
if( carr[i].getName().equals("cempName") )
empName = carr[i].getValue();
if( carr[i].getName().equals("cempDesig") )
empDesig = carr[i].getValue();
}

//storing data into employee table


try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ti
ger");

String vsql = "insert into employee values(?,?,?,?,?,?,?)";


PreparedStatement pstmt = con.prepareStatement(vsql);
//set columns data into positional parameters
pstmt.setInt(1,Integer.parseInt(empId));
pstmt.setString(2,empName);
pstmt.setString(3,empDesig);
pstmt.setString(4,email);
pstmt.setString(5,mobile);
pstmt.setInt(6,Integer.parseInt(empAge));
pstmt.setDouble(7,Double.parseDouble(empSal));

//execute the query


int n = pstmt.executeUpdate();
if( n == 1 ){
out.println("Data stored successfully<br>");
out.println("Do you want insert another record?Goto<a
href=empForm.html>Employee Form</a>");
con.close();
}
}catch(Exception e){
out.println(e.getMessage());
}
%>

HTTP Session Application: ( Login and Logout using Session)


-----------------------------------
index.jsp:
------------
<html>
<head>
<title>Login System</title>
</head>

<body>
<%
String email=(String)session.getAttribute("email");

//redirect user to home page if already logged in


if(email!=null){
response.sendRedirect("home.jsp");
}

String status=request.getParameter("status");

if(status!=null){
if(status.equals("false")){
out.print("Incorrect login details!");
}
else{
out.print("Some error occurred!");
}
}
%>

<form action="loginRequestHandler.jsp">
<table cellpadding="5">
<tr>
<td><b>Email:</b></td>
<td><input type="text" name="email" required/></td>
</tr>

<tr>
<td><b>Password:</b></td>
<td><input type="password" name="password" required/></td>
</tr>

<tr>
<td colspan="2" align="center">
<input type="submit" value="Login"/></td>
</tr>
</table>
</form>

</body>
</html>

loginRequestHandler.jsp:
-------------------------------
<%@page import="com.klu.LoginDAO"%>
<jsp:useBean id="loginBean" class="com.klu.LoginBean" scope="session"/>
<jsp:setProperty name="loginBean" property="*"/>

<%
String result=LoginDAO.loginCheck(loginBean);

if(result.equals("true")){
session.setAttribute("email",loginBean.getEmail());
response.sendRedirect("home.jsp");
}

if(result.equals("false")){
response.sendRedirect("index.jsp?status=false");
}

if(result.equals("error")){
response.sendRedirect("index.jsp?status=error");
}

%>

LoginBean.java:
---------------------
package com.klu;

public class LoginBean {


private String email;
private String password;

public String getEmail() {


return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

DBConnection.java:-
-------------------------
package com.klu;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {


static final String URL="jdbc:oracle:thin:@localhost:1521:xe";
static final String USERNAME="system";
static final String PASSWORD="tiger";

public static Connection getConnection(){


Connection con=null;

try{
Class.forName("oracle.jdbc.driver.OracleDriver");

con=DriverManager.getConnection(URL,USERNAME,PASSWORD);

}catch(Exception e){
e.printStackTrace();
}

return con;
}
}

home.jsp:
------------
<html>
<head>
<title>Login System</title>
</head>

<body>
<%
String email=(String)session.getAttribute("email");

//redirect user to login page if not logged in


if(email==null){
response.sendRedirect("index.jsp");
}
%>

<p>Welcome <%=email%></p>
<a href="logout.jsp">Logout</a>
</body>
</html>

logout.jsp:
-------------
<%
session.invalidate();
response.sendRedirect("index.jsp");
%>

Login , Registration and Logout Application using Http Session:


-------------------------------------------------------------------------------
index.jsp
------------
<html>
<body>
<form method="post" action="login.jsp">
<center>
<table border="1" width="30%" cellpadding="3">
<tr>
<th colspan="2" align=center>Login Form</th>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass"/></td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="submit" value="LOGIN" /></td>
</tr>
<tr>
<td align=center colspan="2">
Yet Not Registered!! <a href="register.jsp">Register Here</a>
</td>
</tr>
</table>
</center>
</form>
</body>
</html>

register.jsp:
---------------
<html>
<body>
<form method="post" action="registration.jsp">
<center>
<table border="1" width="30%" cellpadding="15">
<tr>
<th colspan="2" align="center">
Registration Form
</th>
</tr>
<tr>
<td>Id:</td>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="fname"/></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lname"/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass"/></td>
</tr>
<tr>
<td>Re-type Password</td>
<td><input type="password" name="rpass"/></td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="submit" value="REGISTER" />
</td>
</tr>
<tr>
<td colspan="2" align=center>
Already registered!! <a href="index.jsp">Login Here</a></td>
</tr>
</table>
</center>
</form>
</body>
</html>

registration.jsp:
--------------------
<%@ page import ="java.sql.*" %>
<%
String id = request.getParameter("id");
int i = Integer.parseInt(id);
String user = request.getParameter("uname");
String pwd = request.getParameter("pass");
String rpwd = request.getParameter("rpass");
if( !pwd.equals(rpwd) ) {
out.println("Passwords not match.Register once again");
out.println("<br>Goto <a href=register.jsp>register</a>");
return;
}
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String email = request.getParameter("email");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system", "tiger");
Statement st = con.createStatement();
int n = st.executeUpdate("insert into members
values("+i+",'"+fname+"','"+lname+"','"+email+"','"+user+"','"+pwd+"')");
if (n > 0) {
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("index.jsp");
}
}catch(Exception e){
out.println(e.getMessage());
}
%>

welcome.jsp:
---------------
Registration is Successful.
Please Login Here <a href='index.jsp'>Go to Login</a>

login.jsp:
------------
<%@ page import ="java.sql.*" %>
<%
String userid = request.getParameter("uname");
String pwd = request.getParameter("pass");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system", "tiger");
Statement st = con.createStatement();
String vsql = "select * from members where username='" + userid + "' and
password='" + pwd + "'";
ResultSet rs = st.executeQuery(vsql);
if (rs.next()) {
session.setAttribute("userid", userid);
response.sendRedirect("success.jsp");
} else {
out.println("Invalid password <a href='index.jsp'>try again</a>");
}
%>

success.jsp:
---------------
<%
if ((session.getAttribute("userid") == null) || (session.getAttribute("userid") ==
"")) {
%>
You are not logged in<br/>
<a href="index.jsp">Please Login</a>
<%} else {
%>
Welcome <%=session.getAttribute("userid")%><br>
<a href='logout.jsp'>Log out</a>
<%
}
%>

logout.jsp:
--------------
<%
session.setAttribute("userid", null);
session.invalidate();
response.sendRedirect("index.jsp");
%>

web.xml:
-------------
<web-app>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Potrebbero piacerti anche