Anda di halaman 1dari 52

1) Develop static pages (using only HTML) of an online Book store. The pages should resemble: www.amazon.

com The website should consist the following pages. Home page, Registration and user Login, User profile page, Books catalog, Shopping cart, Payment By credit card, order confirmation.

HOME PAGE: The static home page must contain three frames. Top frame: Logo and the college name and links to Home page, Login page, Registration page, Catalogue page and Cart page (the description of these pages will be given below). Left frame: At least four links for navigation, which will display the catalogue of respective links. For e.g.: When you click the link CSE the catalogue for CSE Books should be displayed in the Right frame. Right frame: The pages to the links in the left frame must be loaded here. Initially this page contains description of the web site. HomePage.html
<html> <head> <title> Online Book Store</title> <frameset rows="20%,*%" scrolling="no"> <frame name="tframe" src="Title.html"> <frameset cols=20%,80%"> <frame name="tframe" src="Links.html"> <frame name="rframe" src="Home.html"> </frameset> </frameset> </head> <body> </body></html>

OutPut:

LOGIN PAGE: LoginPage.html


<html> <head> <title> Online Book Store</title> </head> <body bgcolor="pink"> <center>

<h1>LOGIN</h1> <form action="account.html"> <table border="0"> <tr> <h3><td>Username:</td> <td><input type="text" value="" name="t1"></td></h3> </tr> <tr> <h3><td>Password:</td> <td><input type="password" name="p1"></td></h3> </tr> <tr> <td></td> <td><input type="submit" value="Sign In"> <input type="submit" value="Reset"></td> </tr> </table> </form> </center> </body> </html>

OutPut:

CATOLOGUE PAGE: The catalogue page should contain the details of all the books available in the web site in a table. The details should contain the following: 1. Snap shot of Cover Page. 2. Author Name. 3. Publisher. 4. Price. 5. Add to cart button.

Catologue.html
<html> <head> <title>Online Book Store</title> </head> <body bgcolor="pink"> <h1>Books</h1> <ul type="square" compact> <li>C</li> <li>C++</li> <li>Java</li> <li>DBMS</li> <li>UNIX</li> <li>WT</li> </ul> </body> </html>

OutPut:

REGISTRATION PAGE: Create a registration formwith the following fields 1) Name (Text field) 2) Password (password field) 3) E-mail id (text field) 4) Phone number (text field) 5) Sex (radio button) 6) Date of birth (3 select boxes) 7) Languages known (check boxes English, Telugu, Hindi, Tamil) 8) Address (text area)

RegistrationPage.html

<html> <head> <title> Online Book Store</title> </head> <body bgcolor="pink"> <center> <h1>REGISTRATION</h1> <table border="3"> <tr> <h3><td>Name:</td> <td><input type="text"></td> </tr> <tr> <td>Password:</td>

<td><input type="password"></td> </tr> <tr> <td>Sex:</td> <td><input type="radio" name="r1">Male <input type="radio" name="r1">Female</td> </tr> <tr> <td>Email ID:</td> <td><input type="text"></td> </tr> <tr> <td>Phone no:</td> <td><input type="text"></td> </tr> <tr> <td>Address:</td> <td> <textarea name ="string"></textarea></td> </tr> <tr> <td><input type="submit" value="Sign In"> <input type="reset" value="Reset"></td> </tr> </table> </center> </body> </html>

OutPut:

PAYMENT PAGE: PaymentPage.html


<html> <head> <title> Online Book Store</title> </head> <body bgcolor="pink"> <center> <h1>Card Information</h1> <table border="2">

<tr> <h3><td>Select Card Type:</td> <td><select name=" " size="1"> <option value="type" selected>None</opiton> <option value="credit card">Credit Card</opiton> <option value="master card">Master Card</opiton> <option value="debit card">Debit Card</opiton> </select> </td> </tr> <tr> <td>Enter card no:</td> <td><input type=""></td> </tr> <tr> <td>Name on card:</td> <td><input type="text"></td> </tr> <tr> <td></td> <td><input type="submit" value="Sign In"> <input type="reset" value="Reset"></td> </tr> </table> </center> </body> </html>

OutPut:

2. VALIDATION: Write JavaScript to validate the following fields of the above registration page. 1. Name (Name should contains alphabets and the length >=6 characters). 2. Password (Password should not be less than 6 characters length). 3. E-mail id (Only valid format :name@domain.com) 4. Phone number (Phone number should contain 10 digits only). Registration validation.html: Source code: <html> <head> <title> Welcome to AMAZON website</title> <link rel="stylesheet" href="style.css" type="text/css"> <script type="text/javascript" src="validation.js"> </script> </head> <body> <center> <h3>Registration Form </h3> <br/> <form name="f1" onsubmit="return form1_validation()"> <table cellpadding="1" align="center" > <tr> <td> Name:*</td> <td> <input type="text" name="username"></td></tr> <tr> <td>Choose a ID:*</td> <td> <input type="text" name="id"></td></tr> <tr> <td>Choose a Password:*</td> <td> <input type="password" name="password"></td></tr> <tr> <td>Reenter Password:*</td> <td> <input type="password" name="password1"></td></tr> <tr> <td>Email ID:*</td> <td> <input type="text" name="email"></td></tr> <tr> <td>Phone Number:*</td> <td> <input type="text" name="phno"></td></tr> <tr>

<td valign="top">Gender:*</td> <td> <input type="radio" name="radio" value="1">Male<br> <input type="radio" name="radio" value="2">Female</td></tr> <tr> <td>Date of Birth:*</td> <td>dd: <select name=dd > <option> </option> <script type="text/javascript"> for(i=1;i<=31;i++) document.write("<option value=i>"+i+"</option>"); </script> </select> mm: <select name=mm> <option> </option> <script type="text/javascript"> for(i=1;i<=12;i++) document.write("<option value=i>"+i+"</option>"); </script> </select> yyyy: <select name=yyyy> <option> </option> <script type="text/javascript"> for(i=1880;i<=2008;i++) document.write("<option value=i>"+i+"</option>"); </script> </select> </td></tr> <tr> <td>Material Status:*</th> <td><input type="text" name="mstatus" ></td></tr> <tr> <td>Natinality:*</th> <td><input type="text" name="Nationality" ></td></tr> <tr> <td valign="top">Language Known:*</td> <td> <input type="checkbox" name="checkbox" value="English">English<br> <input type="checkbox" name="checkbox" value="Telugu">Telugu<br> <input type="checkbox" name="checkbox" value="Hindi">Hindi<br> <input type="checkbox" name="checkbox" value="Tamil">Tamil</td></tr> <tr> <td valign="top">Address:*</td>

<td> <textarea name="address"></textarea></td> <tr><td></td><td><input type="submit" value="Submit" hspace="10"> <input type="reset" value="Reset"></td></tr> <tr> <td colspan=2 >*<font color="#FF0000"> fields are mandatory</font ></td></tr> </table> </center> </body> </html>

Validation.js: Source code: function form1_validation() { //Username Validation if(f1.username.value=="") { alert("You must enter a User Name"); f1.username.focus(); return false; } if(f1.username.value.length<3) { alert("User Name must consist of atleast 3 character"); f1.username.focus(); return false; } var checkStr=f1.username.value; for(i=0;i<checkStr.length;i++) if(!(checkStr.charCodeAt(i)>=65&&checkStr.charCodeAt(i)<=91) && !(checkStr.charCodeAt(i)>=97 && checkStr.charCodeAt(i)<=122)) { alert("Please enter valid User Name"); f1.username.focus(); return false; } //Password Validation if(f1.password.value.length<6) { alert("Please enter Password not less than 6"); f1.password.focus();

return false; } if(!(f1.password.value==f1.password1.value)) { alert("Please re-enter corrert Password"); f1.password1.focus(); return false; } //email validation flag=true; if(f1.email.value=="") flag=false; var Str=f1.email.value; if(allValidChars(Str)) for(i=0;i<Str.length;i++) if(Str.charAt(i)=="@") { if((Str.substr(i+1,9)=="yahoo.com")||(Str.substr(i+1,9)=="gmail.com")) { flag=true; break; } } else flag=false; if(!(flag)) { alert("Please enter a valid Email ID"); f1.email.focus(); return false; } //phone number validation flag=true; if(f1.phno.value.length!=10) flag=false; var phno=f1.phno.value; for(i=0;i<phno.length;i++) if(!(phno.charCodeAt(i)>=48&&phno.charCodeAt(i)<=57)) flag=false; if(!flag) { alert("Please enter valid Phone Number"); f1.phno.focus();

return false; } //gender validation flag=false; for(i=0;i<f1.radio.length;i++) if(f1.radio[i].checked) flag=true; if(!(flag)) { alert("Please choose a Gender"); return false; } //Date of birth validation if((f1.dd.selectedIndex<=0)||(f1.mm.selectedIndex<=0)||(f1.yyyy.selectedIndex<=0)) { alert("Please choose a Date of Birth"); f1.phno.focus(); return false; } //checkbox validation flag=false; for (i = 0; i < f1.checkbox.length; i++) { if (f1.checkbox[i].checked) flag = true; } if(!(flag)) { alert("Please select at least one of the \"Language\" options."); return false; } //address validation if(f1.address.value.length<25) { alert("Please enter a Correct Address"); return false; } } function allValidChars(email) { var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_"; for (i=0; i < email.length; i++) { var letter = email.charAt(i).toLowerCase();

if (validchars.indexOf(letter) == -1) return false; } return true; }

Output:

3. Create and save an XML document at the server, which contains 10 users information. Write a program which takes User Id as input and returns the user details by taking the user information from the XML document student.dtd:
<!ELEMENT student(rollnum,name,parntname,branch,year,addr,email,phno)> <!ELEMENT roll_no(#PCDATA)> <!ELEMENT name(first_name,last_name)> <!ELEMENT first_name(#PCDATA)> <!ELEMENT last_name(#PCDATA)> <!ELEMENT father_name(fname,mname,lname)> <!ELEMENT first_name(#PCDATA)> <!ELEMENT last_name(#PCDATA)> <!ELEMENT branch(#PCDATA)> <!ELEMENT year(#PCDATA)> <!ELEMENT address(door_no,street,city)> <!ELEMENT door_no(#PCDATA)> <!ELEMENT street(#PCDATA)> <!ELEMENT city(#PCDATA)> <!ELEMENT email(#PCDATA)> <!ELEMENT phno(#PCDATA)> student.xsd: <xs:schema> <xs:element name="student"> <xs:complextype> <xs:sequence> <xs:element name="roll_no" type="xs:string"/>

<xs:element name="name"> <xs:complextype> <xs:sequence> <xs:element name="first_name" type="xs:string"/> <xs:element name="last_name" type="xs:string"/> </xs:sequence> </xs:complextype> </xs:element> <xs:element name="father-name"> <xs:complextype> <xs:sequence> <xs:element name="first_name" type="xs:string"/> <xs:element name="last_name" type="xs:string"/> </xs:sequence> </xs:complextype> </xs:element> <xs:element name="branch" type="xs:string"/> <xs:element name="year" type="xs:integer"/> <xs:element name="addr"> <xs:complextype> <xs:sequence> <xs:element name="door-no" type="xs:string"/> <xs:element name="street" type="xs:string"/> <xs:element name="city" type="xs:string"/> </xs:sequence>

</xs:complextype> </xs:element> <xs:element name="email" type="xs:string"/> <xs:element name="phno" type="xs:integer"/> </xs:sequence> </xs:complextype> </xs:element> </xs:schema>

student.xml:
<?xml version="1.0" ?> - <student xmlns:xsi="xmlschema-instance" xsi:SchemaLocation="student.xsd"> <roll_no>08A01D2501</roll_no> - <name> <first_name>A</first_name> <Last_name>Aruna</Last_name> </name> - <father_name> <first_name>Venkateswara</first_name> <Last_name>Rao</Last_name> </father_name> <branch>software engineering</branch> <year>1ST Year</year> - <address> <door-no>32-15/1-44</door-no> <street>B.R.Nagar-1</street>

<city>Hyderabad</city> </address> <email>iaaruna@yahoo.com</email> <phno>9949643372</phno> <roll_no>08A01D2501</roll_no> - <name> <first_name>Prathap</first_name> <Last_name>Rudra</Last_name> </name> - <father_name> <first_name>Venkateswara </first_name> <Last_name>B</Last_name> </father_name> <branch> software engineering (mtech) </branch> <year>1st Year</year> - <address> <door-no>door no 603/n</door-no> <street>Recoloney</street> <city>Vijayawada</city> </address> <email>brprathap@gmail.com</email> <phno>9866145423</phno> <roll_no>08A01D2503</roll_no> - <name> <first_name>Ramesh</first_name> <Last_name>Kumar</Last_name> </name>

- <father_name> <first_name>Venkateswara</first_name> <Last_name>Rao</Last_name> </father_name> <branch> software engineering(mtech) </branch> <year>1st Year</year> - <address> <door-no>32-15/1-44</door-no> <street>B.R.Nagar-1</street> <city>Hyderabad</city> </address> <email>itrameshch@gmail.com</email> <phno>9985150379</phno> <roll_no>08A01D2504</roll_no> - <name> <first_name>kishore</first_name> <Last_name>Galla</Last_name> </name> - <father_name> <first_name>Venkateswara</first_name> <Last_name>Rao</Last_name> </father_name> <branch> software engineering (mtech) </branch> <year>1st Year</year> - <address> <door-no>32-15/1-44</door-no> <street>B.R.Nagar-1</street>

<city>hyderabad</city> </address> <email>galla_kishore@yahoo.com</email> <phno>990837394</phno> <roll_no>08A01D2505</roll_no> - <name> <first_name>Madhu</first_name> <Last_name>Garlapati</Last_name> </name> - <father_name> <first_name>Venkateswara</first_name> <Last_name>Rao</Last_name> </father_name> <branch> software engineering (mtech) </branch> <year>1st Year</year> - <address> <door-no>32-15/1-44</door-no> <street>B.R.Nagar-1</street> <city>Hyderabad</city> </address> <email>madhubabu.garlapati@gmail.com</email> <phno>9908005778</phno> </student>

4. Bean Assignments a. Create a JavaBeans which gives the exchange value of INR (Indian Rupees) into equivalent American/Canadian/Australian Dollar value. b. Create a simple Bean with a label which is the count of number of clicks. Then create a Bean info class such that only the count properly is visible in the property Window. c. Create two Beans a) Keypad b) Display pad. After that integrate the two Beans to make it work as a calculator. d. Create two Beans Traffic Light(implemented as a label with only three background colors-red, green, yellow) and Automobile(Implemented as a Text Box which states its state/movement). The state of the Automobile should depend on the following Light Transition table. Developing a Simple Bean Using the BDK: This section presents an example that shows how to develop a simple Bean and connect it to other components via the BDK. Our new component is called the Colors Bean. It appears as either a rectangle or ellipse that is filled with a color. A color is chosen at random when the Bean begins execution. A public method can be invoked to change it. Each time the mouse is clicked on the Bean, another random color is chosen. There is one boolean read/write property that determines the shape. The BDK is used to lay out an application with one instance of the Colors Bean and one instance of the OurButton Bean. The button is labeled Change. Each time it is pressed, the color changes. DEVELOPMENT USING JAVA Create a New Bean Here are the steps that you must follow to create a new Bean: 1. Create a directory for the new Bean. 2. Create the Java source file(s). 3. Compile the source file(s). 4. Create a manifest file. 5. Generate a JAR file. 6. Start the BDK. 7. Test.

The following sections discuss each of these steps in detail. Create a Directory for the New Bean You need to make a directory for the Bean. To follow along with this example, create c:\bdk\demo\sunw\demo\colors. Then change to that directory. The Colors and OurButton Beans Create the Source File for the New Bean The source code for the Colors component is shown in the following listing. It is located in the file Colors.java. The import statement at the beginning of the file places it in the package named sunw.demo.colors. Recall from Chapter 9 that the directory hierarchy corresponds to the package hierarchy. Therefore, this file must be located in a subdirectory named sunw\demo\colors relative to the CLASSPATH environment variable. The color of the component is determined by the private Color variable color, and its shape is determined by the private boolean variable rectangular. The constructor defines an anonymous inner class that extends MouseAdapter and overrides its mousePressed( ) method. The change( ) method is invoked in response to mouse presses. The component is initialized to a rectangular shape of 200 by 100 pixels. The change( ) method is invoked to select a random color and repaint the component. The getRectangular( ) and setRectangular( ) methods provide access to the one property of this Bean. The change( ) method calls randomColor( ) to choose a color and then calls repaint( ) to make the change visible. Notice that the paint( ) method uses the rectangular and color variables to determine how to present the Bean.

// A simple Bean. package sunw.demo.colors; import java.awt.*; import java.awt.event.*; public class Colors extends Canvas { transient private Color color; private boolean rectangular; public Colors() { addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { change(); } }); rectangular = false;

setSize(200, 100); change(); } public boolean getRectangular() { return rectangular; } public void setRectangular(boolean flag) { this.rectangular = flag; repaint(); } public void change() { color = randomColor(); repaint(); } private Color randomColor() { int r = (int)(255*Math.random()); int g = (int)(255*Math.random()); int b = (int)(255*Math.random()); return new Color(r, g, b); } public void paint(Graphics g) { Dimension d = getSize(); int h = d.height; int w = d.width;

g.setColor(color); if(rectangular) { g.fillRect(0, 0, w-1, h-1); } else { g.fillOval(0, 0, w-1, h-1); } } }

Compile the Source Code for the New Bean Compile the source code to create a class file. Type the following: javac Colors.java. Create a Manifest File You must now create a manifest file. First, switch to the c:\bdk\demo directory. This is the directory in which the manifest files for the BDK demos are located. Put the source code for your manifest file in the file colors.mft. It is shown here: Name: sunw/demo/colors/Colors.class Java-Bean: True

This file indicates that there is one .class file in the JAR file and that it is a Java Bean Notice that the Colors.class file is in the package sunw.demo.colors and in the subdirectory sunw\demo\colors relative to the current directory. Generate a JAR File Beans are included in the ToolBox window of the BDK only if they are in JAR files in the directory c:\bdk\jars. These files are generated with the jar utility. Enter the following: jar cfm ..\jars\colors.jar colors.mft sunw\demo\colors\*.class This command creates the file colors.jar and places it in the directory c:\bdk\jars. (You may wish to put this in a batch file for future use.)

Start the BDK Change to the directory c:\bdk\beanbox and type run. This causes the BDK to start. You should see three windows, titled ToolBox, BeanBox, and Properties. The ToolBox window should include an entry labeled Colors for your new Bean. Create an Instance of the Colors Bean After you complete the preceding steps, create an instance of the Colors Bean in the BeanBox window. Test your new component by pressing the mouse anywhere within its borders. Its color immediately changes. Use the Properties window to change the rectangular property from false to true. Its shape immediately changes. Create and Configure an Instance of the OurButton Bean Create an instance of the OurButton Bean in the BeanBox window. Then follow these steps: 1. Go to the Properties window and change the label of the Bean to Change. You should see that the button appearance changes immediately when this property is changed. 2. Go to the menu bar of the BeanBox and select Edit | Events | action | actionPerformed. 3. Move the cursor so that it is inside the Colors Bean display area, and click the left mouse button. You should see the Event Target Dialog dialog box. 4. The dialog box allows you to choose a method that should be invoked when this button is clicked. Select the entry labeled change and click the OK button.You should see a message box appear very briefly, stating that the tool isGenerating and compiling adaptor class. 5. Click on the button. You should see the color change. You might want to experiment with the Colors Bean a bit before moving on. Output:

d) Create two Beans Traffic Light(implemented as a label with only three background colors-red, green, yellow) and Automobile(Implemented as a Text Box which states its state/movement). The state of the Automobile should depend on the following Light Transition table. /*<applet code="Sig.class" height=300 width=200></applet>*/ import java.awt.*; import java.lang.String; import java.awt.event.*; import java.applet.Applet; import java.applet.*; public class Sig extends Applet implements ItemListener{ boolean c1,c2,c3; String s1; Checkbox r1,r2,r3; CheckboxGroup cbg; public void init(){ cbg=new CheckboxGroup(); Panel p=new Panel(); p.setLayout(new GridLayout()); add(r1=new Checkbox("red",cbg,false)); add(r2=new Checkbox("yellow",cbg,false)); add(r3=new Checkbox("green",cbg,false)); r1.addItemListener(this); r2.addItemListener(this); r3.addItemListener(this); } public void paint(Graphics g) { g.setColor(Color.red); g.drawOval(10, 30, 20, 20); //System.out.println(s1);

g.setColor(Color.yellow); g.drawOval(10, 60, 20, 20); g.setColor(Color.green); g.drawOval(10, 90, 20, 20); s1=cbg.getSelectedCheckbox().getLabel(); if(s1=="red") { g.setColor(Color.red); g.fillOval(10, 30, 20, 20); } else if(s1=="yellow") { g.setColor(Color.yellow); g.fillOval(10, 60, 20, 20); } else if(s1=="green") { g.setColor(Color.green); g.fillOval(10, 90, 20, 20); } } public void itemStateChanged(ItemEvent ie) { repaint(); }}

5) a) Install TOMCAT web server and APACHE. While installation assign port number 4040 to TOMCAT and 8080 to APACHE. Make sure that these ports are available i.e., no other process is using this port. b) Access the above developed static web pages for books web site, using these servers by putting the web pages developed in week-1 and week-2 in the document root.

a) Installation of Apache Tomcat 6.0

1) Download the backup program of Apache Tomcat 6.0 from Apache website. Double click on it to start the installation.

2) Then it will display some description about the installation. Click next to continue.

3) Then it will display the list of components to be installed. Dont change

anything click next to continue.

4) Then it will display the installation directory. Click next to continue.

5) Enter the port number as 8080 and give proper username and password. Click next to continue.

6) Java Virtual Machine will be automatically detected by the Installer, once you have installed the JDK software prior to the installation of this web server software.

7) Then the installer finishes its installation. Click on finish to complete the installation.

b) Path setting for servlet-api.jar


1) Right click on my computer then select properties.

2) Then under Advanced tab select Environment variables.

3) click on new then enter Variable name as classpath and enter Variable value as the following. C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;.;

4) Click on OK to complete the Settings.

c) Testing the Tomcat Web Server.


1) Click on StartMenu -> Programs -> Apache Tomcat 6.0 -> Configure Tomcat.

2) Click on start to start the Tomcat service.

3) Then open the Web browser and enter the following in the address to open tomcat homepage. http://localhost:8080

4) Then click on Tomcat Manager and provide the username and password to get into the application managers page.

6) Redo the previous task using JSP by converting the static web pages of assignments 2 into dynamic web pages. Create a database with user information and books information. The books catalogue should be dynamically loaded from the database. Follow the MVC architecture while doing the website. User Authentication: Assume four users user1, user2, user3 and user4 having the passwords pwd1, pwd2, pwd3 and pwd4 respectively. Write a servelet for doing the following. 1. Create a Cookie and add these four user ids and passwords to this Cookie. 2. Read the user id and passwords entered in the Login form (week1) and authenticate with the values (user id and passwords) available in the cookies If he is a valid user(i.e., user-name and password match) you should welcome him by name(user-name) else you should display You are not an authenticated user . Use init-parameters to do this. Store the user-names and passwords in the webinf.xml and access them in the servlet by using the getInitParameters() method.

User Authentication Using Cookies:


Login.html

<html> <head> <title>login.html</title </head>

<body bgcolor="pink"> <center> <h1>Cookies Example</h1> <form action="/Week8Cookies/log"> <table>

<tr> <td><b>userid:</b></td><td><input type="text" name="username"></td> </tr> <tr> <tr> <td><b>password:</b></td><td><input type="text" name="password"></td> </tr> <tr> <td></td> <td><input type="submit" value="Login"><input type="reset" value="Reset"></td> </tr> </table> </form> </center> </body> </html>

AddUsers.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class AddUsers extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html"); PrintWriter out = response.getWriter();

Cookie user1 = new Cookie("John","John123"); Cookie user2 = new Cookie("Johnson","Johnson123"); Cookie user3 = new Cookie("Rahul","Rahul123"); Cookie user4 = new Cookie("Rohit","Rohit123"); Cookie user5 = new Cookie("Raj","Raj123123");

response.addCookie(user1); response.addCookie(user2); response.addCookie(user3); response.addCookie(user4); response.addCookie(user5); out.println("<html><body bgcolor=pink>"); out.println("<center><h1>Cookies added Successfully</h1><center></body></html>"); out.close(); } }

LoginForm.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*;

public class LoginForm extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html"); PrintWriter out = response.getWriter();

Cookie cs[] = request.getCookies(); String userName = request.getParameter("username"); String password = request.getParameter("password"); boolean validUser = false; String cuserName = null; String cpassword = null; for(Cookie c:cs){ cuserName = c.getName(); cpassword = c.getValue(); if(cuserName.equals(userName) && cpassword.equals(password)) {

validUser = true; break; } } if(validUser) { out.println("<html><body bgcolor=pink>"); out.println("<h1>Login Successfull</h1>"); out.println("<b>Welcome " + cuserName+"</b></body></html>"); } else { out.println("<html><body bgcolor=pink>"); out.println("<b>"+userName + "is an invalid user</b></body></html>"); } out.close(); } }

web.xml
<?xml version="1.0"?> <web-app> <servlet> <servlet-name>AddUsersServlet</servlet-name> <servlet-class>AddUsers</servlet-class> </servlet> <servlet-mapping> <servlet-name>AddUsersServlet</servlet-name>

<url-pattern>/add</url-pattern> </servlet-mapping> <servlet> <servlet-name>LoginFormServlet</servlet-name> <servlet-class>LoginForm</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginFormServlet</servlet-name> <url-pattern>/log</url-pattern> </servlet-mapping> </web-app>

Output Screens

1) URL : http://localhost:8085/WeekCookie/add

2) URL : http://localhost:8085/WeekCookie/login.html

3) URL: http://localhost:8085/WeekCookie/log?username=John&password=John123

User Authentication Using Initialization Parameters:


Login.html <html> <head> <title>login.html</title </head> <body bgcolor="pink"> <center> <h1>Cookies Example</h1> <form action="/Week8Init/log"> <table> <tr> <td><b>userid:</b></td><td><input type="text" name="username"></td> </tr> <tr> <tr> <td><b>password:</b></td><td><input type="text" name="password"></td> </tr> <tr> <td></td> <td><input type="submit" value="Login"><input type="reset" value="Reset"></td> </tr> </table> </form> </center> </body> </html>

LoginForm.java

import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class LoginForm extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html"); PrintWriter out = response.getWriter(); Enumeration en = getInitParameterNames(); String userName = request.getParameter("username"); String password = request.getParameter("password"); String initName = null; String initPass = null; boolean validUser=false; while (en.hasMoreElements()) { initName = (String)en.nextElement(); initPass = getInitParameter(initName); if(initName.equals(userName) && initPass.equals(password)) { validUser = true; break; }

} if(validUser) { out.println("<html><body bgcolor=pink>"); out.println("<h1>Login Successfull</h1>"); out.println("<b>Welcome " + userName+"</b></body></html>"); } else { out.println("<html><body bgcolor=pink>"); out.println("<b>"+userName + "is an invalid user</b></body></html>"); } out.close(); } }

web.html
<?xml version="1.0"?> <web-app> <servlet> <servlet-name>LoginFormServlet</servlet-name> <servlet-class>LoginForm</servlet-class> <init-param> <param-name>John</param-name> <param-value>John123</param-value>

</init-param> <init-param> <param-name>Johnson</param-name> <param-value>Johnson123</param-value> </init-param> <init-param> <param-name>Rahul</param-name> <param-value>Rahul123</param-value> </init-param> <init-param> <param-name>Rohit</param-name> <param-value>Rohit123</param-value> </init-param> <init-param> <param-name>Raj</param-name> <param-value>Raj123</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>LoginFormServlet</servlet-name> <url-pattern>/log</url-pattern> </servlet-mapping> </web-app>

OutPut:
URL : http://localhost:8085/Week8Init/Login.html

URL : http://localhost:8085/Week8Init/log?username=Rohit&password=Rohit123

7) Implement the Hello World! program using JSP Struts Framework. Source Code for the Hello World Tag Example <html>

<head> <title>JSP 2.0 Examples - Hello World SimpleTag Handler</title> </head> <body> <h1>JSP 2.0 Examples - Hello World SimpleTag Handler</h1> <hr> <p>This tag handler simply echos "Hello, World!" It's an example of a very basic SimpleTag handler with no body.</p> <br> <b><u>Result:</u></b> <mytag:helloWorld/> </body> </html>
OUT PUT This tag handler simply echos "Hello, World!" It's an example of a very basic SimpleTag handler with no body.

Result: Hello, world!

Anda mungkin juga menyukai