3unit java

34
QUESTION: Develop a web application using HTML &Servlets for the given scenario below: (Required database tables implementation must be done) Write a servlet that enables the user to change the password from an HTML form, as shown in Figure. Suppose that the user information is stored in a database table named Account with three columns: username, password, and name, where name is the real name of the user. The servlet performs the following tasks:

Upload: subasri

Post on 19-Dec-2015

243 views

Category:

Documents


1 download

DESCRIPTION

c x

TRANSCRIPT

QUESTION:

Develop a web application using HTML &Servlets for the given scenario below: (Required database tables implementation must be done) Write a servlet that enables the user to change the password from an HTML form, as shown in Figure. Suppose that the user information is stored in a database table named Account with three columns: username, password, and name, where name is the real name of the user. The servlet performs the following tasks:

Verify that the username and old password are in the table. If not, report the error and redisplay the HTML form. Verify that the new password and the confirmed password are the same. If not, report this error and redisplay the HTML form. If the user information is entered correctly, update the password and report the status of the update to the user, as shown in Figure. AIM:

To develop a web application to change the password using database connectivity.

PROGRAM CODE:index.html<html><head><title>change password</title></head><body><form method="get" action="http://localhost:8080/ses32c/ses32c.jsp"><font face="Times New Roman" size="4"><h1 align="center">Proceed to change password</h1><pre><font face="Times New Roman" size="4">User Name: <input type="text" name="name" value="" /><br>Old Password: <input type="password" name="op" value="" /><br>New Password: <input type="password" name="np" value="" /><br>Confirm Password: <input type="password" name="cp" value="" /><br><input type="submit" value="Change" /> <input type="reset" value="reset" /><br></font></pre></form></body></html>

ses32c.jsp import java.sql.*;import java.sevlet.*;import java.httpservlet.*;public class demo{Public void doGet(HttpServletRequest req, HttpServletResponse res){Pw.printwriter();

String no=request.getParameter("name"); String no1=request.getParameter("op"); String no2=request.getParameter("np");R try{ Class.forName("org.apache.derby.jdbc.ClientDriver"); }catch(ClassNotFoundException ex) { out.println("Class not found"+ex);} try { Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/account","acc","acc"); //con.setAutoCommit(false); Statement st=con.createStatement(); String q="select * from account where name='"+no+"'"; ResultSet rs=st.executeQuery(q); rs.next(); if(rs.getString(2).equals(no1)) { rs.close(); q="update account set newpwd='"+no2+"' where name='"+no+"'"; st.executeUpdate(q); q="update account set oldpwd='"+no2+"' where name='"+no+"'"; st.executeUpdate(q); q="select * from account where name='"+no+"'"; rs=st.executeQuery(q); rs.next(); pw.print("<p align=\"center\"><font face=\"Times New Roman\" size=\"4\" color=\"violet\"><b>Password changed successfully</b></font></p>"); } else { out.print("invalid entry"); } } catch(SQLException e){ pw.println(e); e.getStackTrace() ; }

OUTPUT:

RESULT:Thus the JSP code to change the password using database connectivity is completed successfully and the output is verified.

Date:

QUESTION :

Develop a jsp program that displays factorial value of all the prime numbers from 1 to 100.AIM :

To develop a jsp program that displays factorial value of all the prime numbers from 1 to 100.

PROGRAM :

<HTML>

<HEAD>

<FONT color="blue" size="50"

<B>My Favorite Websites</B>

</FONT>

</HEAD>

<BODY><BR>

<FONT color="purple">ESPN Soccemet</FONT><br></br>

<Font color="red" style=background-color:"yellow">Lord Of The Rings</FONT></BODY><br></br>

<FONT color="blue">yahoo</FONT><br></br>

<FONT COLOR="purple"><B>

Back to Main Page</B></BODY></HTML>

OUTPUT:

RESULT:

Thus the given program to displays factorial value of all the prime numbers from 1 to 100 is done and output is verified successfully.

Ex.No:3.1(B)

Date:

QUESTION :

Design a web application using jsp that performs the following operation (you must have at least two web pages). Design an output jsp page that computes and displays the inverse of the integer given as input from first jsp page and also display the double (twice) of the reversed integer in output jsp page.

AIM :

To create a web application that computes and displays the inverse of the integer given as input from first jsp page and also display the double (twice) of the reversed integer in output jsp page.

PROGRAM :

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

<form name=""fact" action="/f.jsp"/>

<input type="text" name=""f" id="f"/>

<input type="submit" value="submit"/></form> </body></html>

f.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%String n=request.getParameter("f");

int a=Integer.parseInt("f");

%><%! int fact;%><% for(int i=1;i<100;i++){

fact=fact*i;}

%>

Pg1.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html><html> <head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head><body>

<form name="pg" action="http://localhost:8080/rev/pg2.jsp" method="get">

integer<input type="text" name="i" id="i"/>

<input type="submit" value="submit"/> </form> </body></html>

Pg2.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page session="true" %><% int n=Integer.parseInt(request.getParameter("i"));

int n1=n%10;

int n2=n/10;

out.println("reverse value:"+n1+""+n2);

int a=n1*10+n2;

out.println("double"+a*a); %>

OUTPUT:

RESULT :

Thus the given program to that computes and displays the inverse of the integer given as input from first jsp page and also display the double (twice) of the reversed integer in output jsp page is done and output is verified successfully.

QUESTION :

Develop a web application using jsp and session for the following scenario. (You must create at least 3 jsp pages and you should use session object to pass the values) Design a login page that gets the user name and password from the user. If the user name and password matches “AJP” and “123” respectively, it has to display the success message with user name in a different page. Else it has to redirect to the login page.

AIM :

Ex.No:3.1(C)

Date:

To design a login page that gets the user name and password from the user. If the user name and password matches “AJP” and “123” respectively, it has to display the success message with user name in a different page. Else it has to redirect to the login page.

PROGRAM :

pg1.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE 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="pg1" action="/pg2.jsp" method=get">

user name<input type="text" name="u" id="u" />

password<input type="password" name="pwd" id="pwd" />

<input type="submit" value="submit" /></form></body></html>

Pg2.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page session="true" %>

<% String n=request.getParameter("u");%>

<% String n1=request.getParameter("pwd");%>

<% if(request.getParameter("u").equals("ajp")&&request.getParameter("pwd").equals("123"))

{%> <jsp:forward page="/pg3.jsp"/>

<%}else {%>

<jsp:include page="/pg1.jsp"/> <%}%>

Pg3.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title> </head><body><h1>success welcome</h1></body></html>

OUTPUT :

RESULT :

Thus the given program to design a login page that gets the user name and password from the user. If the user name and password matches to display the success message with user name in a different page. Else it has to redirect to the login page is done and the output is verified successfully.

QUESTION :

Design a jsp page to display a form as shown in following figure. Validate all the user data by another jsp page.

AIM :

To design a jsp page and validate the following data.

PROGRAM :

OUTPUT:

RESULT:

Thus the given program to design a jsp page and validate the following data is done and output is verified successfully.

Ex.No:3.2(A)

Date:

QUESTION :

Create a JSP and JDBC-based address book. Your address book should allow one to insert entries, delete entries and search for entries. AIM:To Create a JSP and JDBC-based address book should allow one to insert entries, delete entries and search for entries. PROGRAM:<html><head><title>book</title></head><body>

<form name="myform" action="http://localhost:8084/book/pr1.jsp" method="Post"><fieldset><legend>BOOK</legend><table>

<tr><td>ID:</td><td><input type="text" name="id" id="id"/></td></tr>

<tr><td>Last Name:</td><td><input type="text" name="ln" id="ln"/></td></tr>

<tr><td>First Name:</td><td><input type="text" name="fn" id="fn"/></td></tr>

<tr><td>MI:</td><td><input type="text" name="mi" id="mi" /></td></tr>

<tr><td>ADDRESS:</td><td><input type="text" name="adrs" id="adrs"/></td></tr>

<tr><td>CITY:</td><td><input type="text" name="city" id="city" /></td></tr>

<tr><td>STATE:</td><td><input type="text" name="state" id="state" /></td></tr>

</table></fieldset>

<input type="submit" value="view" name="but" />

<input type="submit" value="insert" name="but" />

<input type="submit" value="delete" name="but"/>

<input type="submit" value="clear" /><br>

Ex.No:3.2(B)

Date:

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

Pr1.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

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

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

String last_name=request.getParameter("ln");

String first_name=request.getParameter("fn");

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

String address=request.getParameter("adrs");

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

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

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

out.println(id);

%><%try{ Class.forName("org.apache.derby.jdbc.ClientDriver");

}catch(ClassNotFoundException e)

{ out.println(e);

}%>

<%try{ Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/priya","pri","priy");

Statement st = con.createStatement();

String q;

//q = "create table crim(id varchar(20),lastname varchar(20),firstname varchar(20),Mi varchar(3),address varchar(20),city varchar(20),state varchar(23),telephone varchar(20))";

//st.execute(q);

// q = "insert into crim values('122','yogith','deepthi','122','xxx','yyy','zzz','098756564')";

//st.executeUpdate(q);

if("insert".equals(but)){

q = "insert into crime values('" + id + "','" + last_name + "','" + first_name + "','" + mi + "','" + address + "','" + city + "','" + state + "')";

st.executeUpdate(q);

out.println("details inserted successfully...!!");

}else if("delete".equals(but)){

out.println("goin to delete");

q = "delete from crime where id='"+id+"' ";// values('" + id + "','" + ln + "','" + fn + "','" + mi + "','" + adrs + "','" + city + "','" + state + "','" + tele + "') where id='"+id+"'";

st.executeUpdate(q);

out.println("details updated successfully...!!");

}else if("view".equals(but)){

q="select * from crime where id='"+id+"' ";

ResultSet rs=st.executeQuery(q);

while (rs.next()) {

out.println("id:"+rs.getString(1) +"<br>last name:"+ rs.getString(2) +"<br>first name:"+ rs.getString(3)+"<br>mi:"+ rs.getString(4)+"<br>address:"+ rs.getString(5)+"<br>city:"+ rs.getString(6)+"<br>state:"+ rs.getString(7)); } }}catch (SQLException e) {

out.println(e);} %>

OUTPUT:

RESULT:Thus the given program to Create a JSP and JDBC-based address book should allow one to insert entries, delete entries and search for entries is done and output is verified successfully.

QUESTION:

Create a Web application for retrieving employee details from database Design the following GUI using JSP and generate the required output by retrieving data from database.

AIM:To create the a Web application for retrieving employee details from database. PROGRAM:New.html:

<html> <head> <title>TODO supply a title</title> </head>

<body><form method="get" action="http://localhost:8084/emp/e1.jsp">

Enter employee no: <input type="text" name="e1" value="" />

<input type="submit" value="Ask oracle" />

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

e2.java:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<%@page import="java.sql.*" %><% String id=request.getParameter("e1"); out.println(id);%><%try{ Class.forName("org.apache.derby.jdbc.ClientDriver"); }catch(ClassNotFoundException e){ System.out.println(e); }try{ Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/emp","e","e"); Statement st = con.createStatement(); String q; //q = "create table employe(id varchar(20),name varchar(20),salary varchar(20),datehired varchar(10))";

Ex.No:3.2(C)

Date:

//st.execute(q); /* q = "insert into employe values('122','yogith','5000','5/5/2012')"; st.executeUpdate(q); q = "insert into employe values('123','arun','5000','2/3/2000')"; st.executeUpdate(q); q = "insert into employe values('124','scott','5000','3/4/2013')"; st.executeUpdate(q); q = "insert into employe values('125','john','5000','2/5/20115')";st.executeUpdate(q);*/ q="select * from employe where id='"+id+"' "; ResultSet rs=st.executeQuery(q); while (rs.next()) { out.println("id:"+rs.getString(1) +"<br> name:"+ rs.getString(2) +"<br>salary name:"+ rs.getString(3)+"<br>hired date:"+ rs.getString(4)); }} catch (SQLException e) {out.println(e); }%><%@ include file="newhtml.html"%> OUTPUT:

RESULT:Thus the given program to create the a Web application for retrieving employee details from database is done and output is verified successfully.

QUESTION :

(Assign grades) Write Bean class that accepts student scores, gets the best score, and then assigns grades based on the marks. Bean class can have methods to data, calculates result and prints it. Access the Bean class from JSP document. Design a HTML/JSP page to read user data.

AIM :

To create a Bean class that accepts student scores, gets the best score, and calculates result and prints it. PROGRAM :

Pg1.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 action="http://localhost:8084/three/pg3.jsp" method="get">

sname1<input type="text" id="s1" name="s1"/>

m1<input type="text" id="m1" name="m1"/>

sname2<input type="text" id="s2" name="s2"/>

m2<input type="text" id="m2" name="m2"/>

sname3<input type="text" id="s3" name="s3"/>

m3<input type="text" id="m3" name="m3"/>

<input type="submit" value="submit"/>

Ex.No:4.1(A)

Date:

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

Valid.java:

package valid;

public class Valid {

String sn1,sn2,sn3;

int sm1,sm2,sm3;

public void setsn1(String x)

{ this.sn1=x;}

public String getsn1() {

return sn1;}

public void setsm1(int x){this.sm1=x; }

public int getsm1() {

return sm1;}

public void setsn2(String x){ this.sn2=x; }

public String getsn2(){

return sn2;}

public void setsm2(int x){this.sm2=x;}

public int getsm2(){

return sm2;}

public void setsn3(String x){

this.sn3=x;}

public String getsn3(){

return sn3;}

public void setsm3(int x){

this.sm3=x;}

public int getsm3(){

return sm3;}

public String validate(){

if(sm1>sm2&&sm1>sm3){

return "the result: student 1 is A grade student 2 is B grade student 3 is C grade";}

else if(sm2>sm1 && sm2>sm3){

return "the result: student 2 is A grade student 1 is B grade student 3 is C grade";}

else if(sm3>sm1&&sm3>sm2){

return "the result:student 3 is A grade student 2 is B grade Student 1 is C grade";}

else

{ return "not valid"; }}}

Pg3.jsp

<%@page language="java" contentType="text/html" %>

<%@page import="valid.Valid" %>

<% String n=request.getParameter("s1");

int n1=Integer.parseInt(request.getParameter("m1"));

String a=request.getParameter("s2");

int a1=Integer.parseInt(request.getParameter("m2"));

String b=request.getParameter("s3");

int b1=Integer.parseInt(request.getParameter("m3"));%>

<jsp:useBean id="bobj" class="valid.Valid" scope="session" >

<% bobj.setsn1(n);

bobj.setsm1(n1);

bobj.setsn2(a);

bobj.setsm2(a1);

bobj.setsn3(b);

bobj.setsm3(b1);

%></jsp:useBean><%=bobj.validate() %>

OUTPUT :

RESULT :

Thus the given program to create a Bean class that accepts student scores, gets the best score, and calculates result is done and the output is verified successfully.

Ex.No:4.1(B)

Date:

QUESTION :

Write a bean class that returns student names in decreasing order of their scores. Access the Bean class from JSP document. Design a HTML/JSP page to prompts the user to enter the number of students, the students’ names, and their scores

AIM :

To create a Bean class that that returns student names in decreasing order of their scores. Access the Bean class from JSP document. PROGRAM :

Valid.java:

package valid;

public class Valid {

String sn1,sn2,sn3;

int sm1,sm2,sm3;

public void setsn1(String x) {

this.sn1=x; }

public String getsn1(){

return sn1;}

public void setsm1(int x){

this.sm1=x;}

public int getsm1(){

return sm1;}

public void setsn2(String x){

this.sn2=x;}

public String getsn2(){

return sn2;}

public void setsm2(int x){

this.sm2=x;}

public int getsm2(){

return sm2;}

public void setsn3(String x){

this.sn3=x;}

public String getsn3(){

return sn3;}

public void setsm3(int x){

this.sm3=x;}

public int getsm3(){

return sm3; }

public String validate(){

if(sm1>sm2&&sm1>sm3&&sm2>sm3) {

return sn1+" "+sn2+" " +sn3;}

else if(sm2>sm1&&sm2>sm3&&sm1>sm3){

return sn2+" "+sn1+" "+sn3;}

else{

return sn3+" " +sn2+" " +sn1;}}}

dec1.html:

</head>

<body>

<form action="http://localhost:8084/dec/dec3.jsp" method="get">

sname1<input type="text" id="s1" name="s1"/>

m1<input type="text" id="m1" name="m1"/>

sname2<input type="text" id="s2" name="s2"/>

m2<input type="text" id="m2" name="m2"/>

sname3<input type="text" id="s3" name="s3"/>

m3<input type="text" id="m3" name="m3"/>

<input type="submit" value="submit"/>

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

Dec3.jsp:

<%@page language="java" contentType="text/html" %>

<%@page import="valid.Valid" %>

<% String n=request.getParameter("s1");

int n1=Integer.parseInt(request.getParameter("m1"));

String a=request.getParameter("s2");

int a1=Integer.parseInt(request.getParameter("m2"));

String b=request.getParameter("s3");

int b1=Integer.parseInt(request.getParameter("m3"));%>

<jsp:useBean id="bobj" class="valid.Valid" scope="session" >

<% bobj.setsn1(n);

bobj.setsm1(n1);

bobj.setsn2(a);

bobj.setsm2(a1);

bobj.setsn3(b);

bobj.setsm3(b1); %>

</jsp:useBean><%=bobj.validate() %>

OUTPUT :

RESULT :

Thus the given program To create a Bean class that that returns student names in decreasing order of their scores is done and output is verified successfully.

QUESTION :

Create a bean class that checks whether two words are anagrams. Two words are anagrams if they contain the same letters in any order.. Bean class can have methods to accept data, calculates result and prints it. Access the Bean class from servlet document. Design a HTML/JSP page to read user data.

AIM :

Create a bean class that checks whether two words are anagrams.

PROGRAM :

A1.html:

</head><body>

<form action="http://localhost:8084/ana/a3.jsp" method="get">

string1<input type="text" id="s1" name="s1"/>

string2<input type="text" id="s2" name="s2"/>

<input type="submit" value="submit"/>

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

package A2;

import java.util.Arrays;

import java.util.Scanner;

Ex.No:4.1(C)

Ex.No:3.1(A)

public class a2 {

String s1,s2;

public void sets1(String s1){

this.s1=s1; }

public String gets1() {

return s1; }

public void sets2(String s2) {

this.s2=s2;}

public String gets2(){

return s2; }

public String validate(){

String a="",b="";

char ana[]=s1.toCharArray();

Arrays.sort(ana);

char gana[]=s2.toCharArray();

Arrays.sort(gana);

if(ana.length==gana.length){

for(int i=0;i<ana.length;i++){

a=a+ana[i];

b=b+gana[i];}

if(a.equals(b)){

return s1+" "+s2+" "+" is a anagram";}

else{

return s1+" "+s2+" "+" is not a anagram";} }else

return "its not a anagram "; } }

OUTPUT :

RESULT :

Thus the given program to create a bean class that checks whether two words are anagrams is done and output is verified successfully.