You are on page 1of 9

T 807 Internet Lab valley

Department of Information Technology vijayagiri School of Engineering and Technology, viyajagiri

JSP pages use several delimiters for scripting functions. The most basic is <% ... %>, which encloses a JSP scriptlet. A scriptlet is a fragment of Java code that is run when the user requests the page. Other common delimiters include <%= ... %> for expressions, where the value of the expression is placed into the page delivered to the user, and directives, denoted with < %@ ... %>. Java code is not required to be complete or self-contained within its scriptlet element block, but can straddle markup content providing the page as a whole is syntactically correct. For example, any Java if/for/while blocks opened in one scriptlet element must be correctly closed in a later element for the page to successfully compile. Markup which falls inside a split block of code is subject to that code, so markup inside an if block will only appear in the output when the if condition evaluates to true; likewise, markup inside a loop construct may appear multiple times in the output depending upon how many times the loop body runs. ALGORITHM Step 1: Start Step 2: Create an HTML page which provides an interface for the user to enter the username and password, if user in administrator goto step 5 else goto step 3 Step 3: Display the survey questions and options. Step 4: On submit the results should be updated on a table and a thank you message should be printed Step 5: Display the survey questions and the result. Step 6: Stop PROGRAM DEVELOPMENT Login.html <script language="javascript"> function validate(form1) { if((form1.login.value=="")||(!isNaN(form1.login.value))) { alert("Please enter a valid login id"); form1.login.select(); form1.login.value=""; form1.login.focus(); return(false); } if(form1.pass.value=="") { alert("Please enter your password"); form1.pass.select(); form1.pass.value=""; form1.pass.focus(); return(false); } return(true);
University Register No: 75339

T 807 Internet Lab valley

Department of Information Technology vijayagiri School of Engineering and Technology, viyajagiri

response.sendRedirect("ad.html"); } } else if((login.equals(user))&&(pass.equals(passw))) { out.println("inside"); response.sendRedirect("user.html"); } } } %> Ad.html <html> <head> <title>onlineadmin</title> </head> <body> <center><h3><font color="green">Survey results</font></h3></center> <a href="ansget.jsp">Click here for results</a> </body> </html> Online.html: <html> <head> <title>Online Survey</title> </head> <body> <h1><font color=green>Welcome to Online Survey</font></h1> <form name="form1" method="post" action="reg2.jsp"> Do you think we need a change in our current political system? &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <input name="1" type="radio" value="yes">yes <input name="1" type="radio" value="no">no <br></br> Do you think the persent educational system is needs a makeover? &nbsp <input name="2" type="radio" value="yes">yes <input name="2" type="radio" value="no">no <br></br> Do you think the roads of Kerala is safe for driving? &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <input name="3" type="radio" value="yes">yes
University Register No: 75339

T 807 Internet Lab valley

Department of Information Technology vijayagiri School of Engineering and Technology, viyajagiri

PROGRAM DEVELOPMENT UserCheck Java Program import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class UserCheck extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try { res.setContentType("text/html"); PrintWriter out=res.getWriter(); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String sDBQ = "C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/ROOT/login.mdb"; String database ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + sDBQ+ ";DriverID=22;READONLY=false"; Connection cn = null; ResultSet rs = null; Statement st=null; cn = DriverManager.getConnection(database); String user=req.getParameter("nm"); String pass=req.getParameter("pwd"); String qry="SELECT * FROM login"; st = cn.createStatement(); rs = st.executeQuery( qry ); ResultSetMetaData rsmd = rs.getMetaData(); int n = rsmd.getColumnCount(); boolean b=false; int i=1; while(rs.next()) { if(user.equals(rs.getString(1))) { if(pass.equals(rs.getString(2))) { b=true; out.println( "<CENTER><H3>Welcome "+user+"</H3></CENTER>" ); out.println("<CENTER><AHREF=../index.html>Home</A></CENTER>"); } else { b=true;
University Register No: 75339

T 807 Internet Lab valley

Department of Information Technology vijayagiri School of Engineering and Technology, viyajagiri

out.println( "<CENTER><H3>Wrong Password</H3></CENTER>" ); out.println("<BR /> <BR /> <CENTER><A HREF=../index.html>Home</A></CENTER>"); } } } if(b==false) { out.println("<CENTER><H3>Invalid User</H3></CENTER>"); out.println("<BR /> <BR /> <CENTER><A HREF=../register.html>Click Here for Register</A></CENTER>"); } } catch(Exception e) { System.out.println(e); } } UserRegister Java Program import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class UserRegister extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try { res.setContentType("text/html"); PrintWriter out=res.getWriter(); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String sDBQ = "C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/ROOT/login.mdb"; String database ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + sDBQ+ ";DriverID=22;READONLY=false"; Connection cn = null; ResultSet rs = null; Statement st=null; cn = DriverManager.getConnection(database); String user=req.getParameter("nm"); String pass=req.getParameter("pwd"); String qry="SELECT * FROM login"; st = cn.createStatement();
University Register No: 75339

T 807 Internet Lab valley

Department of Information Technology vijayagiri School of Engineering and Technology, viyajagiri

rs = st.executeQuery( qry ); ResultSetMetaData rsmd = rs.getMetaData(); int n = rsmd.getColumnCount(); boolean b=false; int i=1; while(rs.next()) { if(user.equals(rs.getString(1))) { b=true; out.println("<CENTER><H3>Error, User name already taken</H3></CENTER>"); out.println("<BR /> <BR /> <CENTER><A HREF=../register.html>Click Here for Register</A></CENTER>"); out.println("<CENTER><AHREF=../index.html>Home</A></CENTE R>"); } } if(b==false) { PreparedStatement stp=cn.prepareStatement("insert into login values(?,?)"); stp.setString(1,user); stp.setString(2,pass); stp.executeUpdate(); out.println(" <CENTER><H3>Congrats "+user+", You are successfully registered</H3></CENTER>"); out.println("<BR /> <BR /> <CENTER><A HREF=../index.html>Home</A></CENTER>"); } } catch(Exception e) { System.out.println(e); } } web.xml <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>My Servlet</display-name> <description>My Servlet</description> <servlet> <servlet-name>UserCheck</servlet-name> <servlet-class>UserCheck</servlet-class> </servlet> <servlet-mapping>
University Register No: 75339

T 807 Internet Lab valley

Department of Information Technology vijayagiri School of Engineering and Technology, viyajagiri

<servlet-name>UserCheck</servlet-name> <url-pattern>/servlet/UserCheck</url-pattern> } </script> <html> <head> <title>USER LOGIN</title> <head> <body> <form name="form1" action="check.jsp" method=post onSubmit="return validate(this)"> <center> <table> <tr> <td>LOGIN ID:</td><td><input type="text" name="login"></td> </tr> <tr> <td>PASSWORD:</td><td><input type="password" name="pass"></td> </tr> <tr> <td><input type="submit" value="Submit"></td> </tr> </table> </center> </form> </body> </html> Check.jsp <%@page import="java.sql.*"%> <%@page import="java.util.*"%> <%@page import="java.io.*"%> <% String login=""+request.getParameter("login"); String pass=""+request.getParameter("pass"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:details"); Statement stmt=con.createStatement(); String st="select * from user"; ResultSet rs=stmt.executeQuery(st); while(rs.next()) { String user=rs.getString("username"); String passw=rs.getString("password"); if(login.equals("admin")) { out.println("inside"); if((login.equals(user))&&(pass.equals(passw))) {
University Register No: 75339

T 807 Internet Lab valley

Department of Information Technology vijayagiri School of Engineering and Technology, viyajagiri

out.println("inside"); the problem to the client. Internet Explorer 5, however, typically replaces servergenerated error messages with a canned page that it considers friendlier. You will need to turn off this feature when debugging JSP pages. To do so with Internet Explorer 5, go to the Tools menu, select Internet Options, choose the Advanced tab, and make sure Show friendly HTTP error messages box is not checked. Aside from the regular HTML, there are three main types of JSP constructs that you embed in a page: scripting elements, directives, and actions. Scripting elements let you specify Java code that will become part of the resultant servlet, directives let you control the overall structure of the servlet, and actions let you specify existing components that should be used and otherwise control the behavior of the JSP engine. ALGORITHM Step 1: Start. Step 2: Create an html page with name, login id, password, date of birth and email. Step 3: Perform validation for login id, password and email using java script. Step 4: Create a database and a table in MS-Access. Step 5: Create a jsp file to link to the database and html page. Step 6: Stop; PROGRAM DEVELOPMENT reg.html <html> <head><title>Registration form</title></head> <body> <script language="javascript" type="text/javascript"> function sub() { var name=form1.nam.value; var id=form1.id.value; var pass=form1.pass.value; var day=form1.day.value; var month=form1.month.value; var year=form1.year.value; var email=form1.email.value; //validation for login id var f=0; var p=0; var numaric = id; for(var j=0; j<numaric.length; j++)
University Register No: 75339

T 807 Internet Lab valley

Department of Information Technology vijayagiri School of Engineering and Technology, viyajagiri

{ var alphaa = numaric.charAt(j); var hh = alphaa.charCodeAt(0); if((hh > 47 && hh<58)) { f=1; } if((hh > 64 && hh<91) || (hh > 96 && hh<123)) { p=1; } } if((f==0)||(p==0)) { alert("Your Login ID should be alphanumeric"); return false; } //validation for password var f=0; var p=0; var k=0; var numaric = pass; for(var j=0; j<numaric.length; j++) { var alphaa = numaric.charAt(j); var hh = alphaa.charCodeAt(0); if((hh > 47 && hh<58)) { f=1; } if((hh > 64 && hh<91) || (hh > 96 && hh<123)) { p=1; } if((alphaa=="!")||(alphaa=="@")||(alphaa=="#")||(alphaa=="$")|| (alphaa=="%")||(alphaa=="^")||(alphaa=="&")||(alphaa=="*")||(alphaa=="+")|| (alphaa=="=")||(alphaa=="-")||(alphaa=="_")||(alphaa=="/")||(alphaa==";")) { k=1; } } if((f==0)||(p==0)||(k==0))
University Register No: 75339

T 807 Internet Lab valley

Department of Information Technology vijayagiri School of Engineering and Technology, viyajagiri

{ alert("Your password should be alphanumeric with a special character"); return false; } //validation for email var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; if(emailPattern.test(email)==0) { alert("Your email id is not in correct format"); } } function re() { form1.nam.value=""; form1.id.value=""; form1.pass.value=""; form1.email.value=""; form1.day.value=""; form1.month.value=""; form1.year.value=""; } </script> <form id=form1 method="post" action="reg_jsp.jsp"> <b>Name</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input name="nam" type="text" value=""/><br></br> <b>Login ID</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input name="id" type="text" value=""/><br></br> <b>Password</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input name="pass" type="password" value=""/><br></br> <b>Date-birth</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp day<select name="day"> <option value=""> </option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option>

University Register No: 75339

You might also like