VISUAL BEANS - madsinc.weebly.commadsinc.weebly.com/uploads/1/2/2/6/12263565/visual_be… · Web...
Transcript of VISUAL BEANS - madsinc.weebly.commadsinc.weebly.com/uploads/1/2/2/6/12263565/visual_be… · Web...
VISUAL BEANSAim:
Create a simple visual bean with a area filled with a color.The shape of the area depends on the property shape. If it is set to true then the shape of the area is Square and it is Circle, if it is false.The color of the area should be changed dynamically for every mouse click. The color should also be changed if we change the color in the “property window “.
Program:
package sunw.demo.mycolors;
import java.awt.*;import java.awt.event.*;public class Colors extends Canvas {transient private Color color;private boolean square;public Colors() {addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent me) {change();}});square = false;setSize(100, 100);change();}public boolean getSquare() {return square;}public void setSquare(boolean flag) {this.square = 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(square) {g.fillRect(0, 0, w-1, h-1);}else {g.fillOval(0, 0, w-1, h-1);}}}output:
Output
Aim:Installing Apache Tomcat web server
1. From C:\Software launch apache-tomcat-6.0.14.exe.
2. At the Welcome screen click Next.
3. In the Licence agreement screen, click I Agree.
4. In the Choose Components screen, click Next.
5. Leave the default Destination Folder in the Choose Install Location screen and click Next.
6. In the Configuration screen, leave the defaults of HTTP/1.1 Connector Port as 8080, User Name as admin and “empty” in password and click Next.
7. In the Java Virtual Machine screen, leave the default path and click Install.
8. When the Completing the Apache Tomcat Setup Wizard message appears, uncheck Show Readme and click Finish.
9. Verify that Apache Tomcat is started as shown below.
3.Aim: USER AUTHENTICATION(cookies program)
Assume four users user1, user2, user3 and user4 having the passwords pwd1,pwd2, pwd3 and pwd4 respectively. Write a servelet for doing the following.
a. Create a Cookie and add these four user id’s and passwords to this Cookie.b. 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.
Procedure:
Create an html file to collect four user name and passwords and name it addusers.html
Create a servlet by name Addcookie.java. The servlet should use Cookie object to store user name and password for four users.
Compile AddCookies. java file and place the resulting .class file in web/WEB-INF/classes folder. (if WEB-INF folder is not available you need to create that folder and also classes folder inside it).
Edit the web.xml file in WEB-INF folder to include following tags
<servlet><servlet-name>AddCookiesServlet</servlet-name><servlet-class>AddCookiesServlet</servlet-class></servlet><servlet-mapping><servlet-name>AddCookiesServlet</servlet-name><url-pattern>/servlet/AddCookiesServlet</url-pattern></servlet-mapping>
Edit the Addusers.html file. In the form tag provide the action attribute. The action attribute should refer to the Addcookie servlet. i.e (action=http://localhost:8080/web/servlet/AddCookies)
Open the browser and in the address bar type http://localhost:8080/web/Addusers.html and enter the names, passwords for four users.
Create a servlet by name AuthCookies.java. The servlet should compare the username/passwords stored in the cookie with the ones provided by the user in the login form. Compile the Servlet and place the resulting .class file in the WEB-INF/classes folder.
Edit the login.htm file. Provide the action attribute to point to the AuthCookies servlet. (action=http://localhost:8080/web/servlet/AddCookies)
Edit the web.xml file in WEB-INF folder to include following tags<servlet-name>AuthCookies</servlet-name><servlet-class>AuthCookies</servlet-class></servlet><servlet-mapping><servlet-name>AuthCookies</servlet-name><url-pattern>/servlet/AuthCookies</url-pattern></servlet-mapping>
Program
Using Cookies
<html><head><title>Login Page</title></head><body><form method="post" action="http://localhost:8080/web/servlet/AuthCookies"><p align="center"> Login : <input type="text" name="userid" maxlength="8" size="16"><p align="center"> Password : <input type="password" name="password" maxlength="8" size="16"><br><br><p align="center"> <input type="submit" value="Submit">
</form></body></html>
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>AddCookiesServlet</servlet-name> <servlet-class>AddCookiesServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AddCookiesServlet</servlet-name> <url-pattern>/servlet/AddCookiesServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>AuthCookies</servlet-name> <servlet-class>AuthCookies</servlet-class> </servlet> <servlet-mapping> <servlet-name>AuthCookies</servlet-name> <url-pattern>/servlet/AuthCookies</url-pattern> </servlet-mapping>
AuthCookies.java
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;
public class AuthCookies extends HttpServlet{
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ Cookie[] cookies = request.getCookies();String user_id = request.getParameter("userid");String pass_word = request.getParameter("password");
PrintWriter pw = response.getWriter();boolean authentic = false;
for(int i=0;i<cookies.length;i=i+2){
String uid_name = cookies[i].getName();String uid_value = cookies[i].getValue();String pass_name = cookies[i+1].getName();String pass_value = cookies[i+1].getValue();
if(uid_name.contentEquals("userid1") && uid_value.contentEquals(user_id) && pass_name.contentEquals("pass1") && pass_value.contentEquals(pass_word)){ authentic=true;
break;}
if(uid_name.contentEquals("userid2") && uid_value.contentEquals(user_id) && pass_name.contentEquals("pass2") && pass_value.contentEquals(pass_word)){ authentic=true;break;}
if(uid_name.contentEquals("userid3") && uid_value.contentEquals(user_id) && pass_name.contentEquals("pass3") && pass_value.contentEquals(pass_word)){ authentic=true;break;}
if(uid_name.contentEquals("userid4") && uid_value.contentEquals(user_id) && pass_name.contentEquals("pass4") && pass_value.contentEquals(pass_word)){ authentic=true;break;}}
if(authentic)pw.write("<B>Welcome "+user_id);elsepw.write("<B>You are not an authenticated user");}
}
AddCookies.java
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;
public class AddCookiesServlet extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String uid1 = request.getParameter("userid1");String pwd1 = request.getParameter("pass1");
Cookie userid1 = new Cookie("userid1",uid1);Cookie password1 = new Cookie("pass1",pwd1);
response.addCookie(userid1);response.addCookie(password1);
String uid2 = request.getParameter("userid2");String pwd2 = request.getParameter("pass2");
Cookie userid2 = new Cookie("userid2",uid2);Cookie password2 = new Cookie("pass2",pwd2);
response.addCookie(userid2);response.addCookie(password2);
String uid3 = request.getParameter("userid3");String pwd3 = request.getParameter("pass3");
Cookie userid3 = new Cookie("userid3",uid3);Cookie password3 = new Cookie("pass3",pwd3);
response.addCookie(userid3);response.addCookie(password3);
String uid4 = request.getParameter("userid4");String pwd4 = request.getParameter("pass4");
Cookie userid4 = new Cookie("userid4",uid4);Cookie password4 = new Cookie("pass4",pwd4);
response.addCookie(userid4);response.addCookie(password4);
}}
AddUsers.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head><link rel="StyleSheet" href="./styles.css" type="text/css" media="screen"><style type="text/css">body {background-color:gold;}</style> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <form name="Form1" method="post" action="http://localhost:8080/web/servlet/AddCookiesServlet"> <table border="1"> <tr> <td colspan="4" align="center">
<h1>Enter four users</h1> </td> <tr> <td> <b>Enter Userid </b> </td> <td><input type="textbox" name="userid1" size="25" value=""> </td> <td> <b> Enter Password</b> </td> <td><input type="password" name="pass1" size="25" value=""></td></tr> <tr><td><b>Enter Userid </b></td> <td> <input type="textbox" name="userid2" size="25" value=""> </td> <td><b> Enter Password</b> </td> <td><input type="password" name="pass2" size="25" value=""></td></tr> <tr><td> <b>Enter Userid </b> </td><td><input type="textbox" name="userid3" size="25" value=""></td> <td><b> Enter Password</b> </td> <td><input type="password" name="pass3" size="25" value=""></td></tr> <tr><td><b>Enter Userid </b></td><td><input type="textbox" name="userid4" size="25" value=""></td>
<td><b> Enter Password</b></td><td><input type="password" name="pass4" size="25" value=""></td></tr> <tr><td colspan="4" align="center"><input type="submit" value="submit"></td> </form>
</body></html>
Output