Pages

Tuesday 29 October 2019

student registration form in html and jsp

--------------------------------
--------------------------------------------
----------------------------------------------------



 student Registration Form(HTML file)


<html>
<head>
<title>student Registration Form</title>
</head><center>
<body>
<h1>student Register Form</h1>
<form action="main.jsp" method="post">
            <table style="with: 50%">
                <tr>
                    <td>First Name</td>
                    <td><input type="text" name="first_name" /></td>
                </tr>
                <tr>
                    <td>Last Name</td>
                    <td><input type="text" name="last_name" /></td>
                </tr>
                <tr>
                    <td>phone</td>
                    <td><input type="text" name="phone" /></td>
                </tr>
                    <tr>
                    <td>Email</td>
                    <td><input type="text" name="email" /></td>
                </tr>
              
                    <tr>
                    <td>gender</td>
                    <td><input type="radio" name="gender" value="male"/>
                    male
                    <input type="radio" name="gender" value="female"/>
                    female</td>
                </tr>
                <tr>
                    <td>branch</td>
                    <td><select name="branch">
                        <option value="mca">mca</option>
                        <option value="ms.c">ms.c</option>
                        <option value="mba">mba</option>
                        </td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td><textarea name="address"></textarea></td>
                </tr>
                </table>
              <h3>  Enter Marks</h3>
        <table style="with: 50%" border="1" >
                <tr >
                    <td>math</td>
                    <td>java</td>
                    <td>ooad</td>
                    <td>itej</td>
                </tr>
                <tr>
                    <td><input type="text" name="math" /></td>
                    <td><input type="text" name="java" /></td>
                    <td><input type="text" name="ooad" /></td>
                    <td><input type="text" name="itej" /></td>
                </tr>
           
      
                </table>
            <input type="submit" value="Submit" /></form></center>
</body>
</html>

student Registration Form(jsp file)

<html>
<head>
<title>student Registration Form</title>
</head><center>
<body>
<h1>student Register Form</h1>
            <table style="with: 50%">
                <tr>
                    <td>First Name</td>
                    <td><%request.getParameter("First_name")%></td>
                </tr>
                <tr>
                    <td>Last Name</td>
                    <td><%request.getParameter("Last_name")%></td>
                </tr>
                <tr>
                    <td>phone</td>
                    <td><%request.getParameter("phone")%></td>
                </tr>
                    <tr>
                    <td>Email</td>
                    <td><%request.getParameter("mail")%></td>
                </tr>
              
                    <tr>
                    <td>gender</td>
                    <td><%request.getParameter("gender")%></td>
                </tr>
                <tr>
                    <td>branch</td>
                    <td><%request.getParameter("branch")%></td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td><%request.getParameter("address")%></td>
                </tr>
                </table>
              <h3>  Enter Marks</h3>
        <table style="with: 50%" border="1" >
                <tr >
                    <td>math</td>
                    <td>java</td>
                    <td>ooad</td>
                    <td>itej</td>
                </tr>
                <tr>
                    <td><%request.getParameter("math")%></td>
                    <td><%request.getParameter("java")%></td>
                    <td><%request.getParameter("ooad")%></td>
                    <td><%request.getParameter("itej")%></td>
                </tr>
           
      
                </table>
            </center>
</body>
</html>

output

 


5 example on jsp

example 1
__________________
<html> <body>
<% out.print("Welcome to KLN JSP Class\n"); %>
</body>
</html>
___________________
example 2
__________________
<html> <body>
<% out.print("Addition Of two number\n"); %>
<br>
<% int a=5, b=10, c; c=a+b;
out.println("Sum of Two Numbers =" + c); %>
</body>
</html>
__________________
example 3
__________________
<html> <body>
<h3> <% String str1 ="Mr. KLN"; %>
<% String str2 =""; %>
Welcome to <%=str1 %> </h3>
<% if(str1.equals("Mr. KLN"))
out.println("Hi "+str1);
else
out.println("Hello ");
%>
</body>
</html>
__________________
example 4
__________________
<html> <body>
<p>Today's date:
<%= (new java.util.Date()).toLocaleString()%>
<br> CLASS Time : <%= 7+2 %> : <%= 20 %>
</p> </body> </html>
__________________
example 5
__________________
<html>
<body>
<%!intdata=50;%>
<%="Valueofthevariableis:"+data%>
</body>
</html>

Monday 28 October 2019

Register

<html> <body>
<center>
<table>
<form action= "servlet/Register" method="post">
<tr> <td> Name: <td><input type="text" name="userName"/><br/><br/>
<tr> <td> Password: <td> <input type="password" name="userPass"/><br/><br/>
<tr> <td> Email Id:<td><input type="text" name="userEmail"/><br/><br/>
<tr> <td> Country: <td>
<select name="userCountry">
<option>India</option>
<option>Pakistan</option>
<option>other</option>
</select>
<br/><br/>
<input type="submit" value="register"/>
</form> </body> </html>



Register.java
88
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/servlet/Register")
public class Register extends HttpServlet{
public void doPost(HttpServletRequestrequest, HttpServletResponseresponse)
throws ServletException, IOException{
response.setContentType("text/html");
PrintWriterout = response.getWriter();
String n=request.getParameter("userName");
String p=request.getParameter("userPass");
String e=request.getParameter("userEmail");
String c=request.getParameter("userCountry");
Class.forName("oracle.jdbc.driver.OracleDriver");Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system","oracle");PreparedStatementps=con.prepareStatement("insert into registeruservalues(?,?,?,?)");ps.setString(1,n);ps.setString(2,p);ps.setString(3,e);ps.setString(4,c);inti=ps.executeUpdate();if(i>0)out.print("You are successfully registered...");}catch (Exception e2) { System.out.println(e2); }out.close();}}


web.xml
90
<web-app>
<servlet>
<servlet-name>registerpage</servlet-name>
<servlet-class>Register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>registerpage</servlet-name>
<url-pattern>/servlet/Register</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Register.html</welcome-file>
</welcome-file-list>
</web-app>


ServletDatabase Connection

importjava.io.*;
importjava.sql.*;
importjavax.servlet.*;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.*;
@WebServlet("/IT2017/DBConnReuse")//XMLfilenotrequired
publicclassDBConnReuseextendsHttpServlet{
privateConnectioncon=null;
publicvoidinit(ServletConfigconfig)throwsServletException
{super.init(config);
try{Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
}catch(Exceptione){System.out.println(e);}}


publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException
{response.setContentType("text/html");
PrintWriterout=response.getWriter();
out.println("<html><body><h1>StudentDetails.</h1>");
Statementstmt;
try{stmt=con.createStatement();
ResultSetrs=stmt.executeQuery("Select*fromstud");
out.println("<html><body>");
while(rs.next())
out.println(rs.getString(1)+""+rs.getString(2));
out.println("</body></html>");
}catch(SQLExceptione){out.println(e);}
}
publicvoiddestroy()
{try{
if(con!=null)con.close();
}catch(Exceptione){System.out.println(e);}
}
}

Login Logout using Cookies

link.html

<html>
<a href="login.html">Login</a>
<a href="LogoutServlet">Logout</a>
<a href="ProfileServlet">Profile</a>
</html>

login.html

<form action="LoginServlet" method="post">
Name:<input type="text" name="name"><br>
Password:<input type="password" name="password"><br>
<input type="submit" value="login">
</form>

LoginServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServletextends HttpServlet{
protected void doPost(HttpServletRequestrequest, HttpServletResponseresponse)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriterout=response.getWriter();
String name=request.getParameter("name");
String password=request.getParameter("password");
if(password.equals("admin123")){
out.print("You are successfully logged in!");
out.print("<br>Welcome, "+name);
Cookie ck=new Cookie("name",name);
response.addCookie(ck);
request.getRequestDispatcher("link.html").include(request, response);
}else{
out.print("sorry, username or password error!");
request.getRequestDispatcher("login.html").include(request, response);
}
out.close();
}
}

LogoutServlet.java

import java.io.*; import javax.servlet.*; import javax.servlet.http.*;
publicclassLogoutServletextendsHttpServlet{
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html");
PrintWriterout=response.getWriter();
request.getRequestDispatcher("link.html").include(request,response);
Cookieck=newCookie("name","");
ck.setMaxAge(0);
response.addCookie(ck);
out.print("youaresuccessfullyloggedout!");
}
}

ProfileServlet

public class ProfileServletextends HttpServlet{
protected void doGet(HttpServletRequestrequest, HttpServletResponseresponse)
throws ServletException, IOException{
response.setContentType("text/html");
PrintWriterout=response.getWriter();
Cookie ck[]=request.getCookies();
if(ck!=null){
String name=ck[0].getValue();
if(!name.equals("")||name!=null){
out.print("<b>Welcome to Profile</b>");
out.print("<br>Welcome, "+name);
request.getRequestDispatcher("link.html").include(request, response);
}else{
out.print("Please login first");
request.getRequestDispatcher("login.html").include(request, response);
}
out.close();
}
}

web.xml
<web-app>
<servlet>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<display-name>ProfileServlet</display-name>
<servlet-name>ProfileServlet</servlet-name>
<servlet-class>ProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProfileServlet</servlet-name>
<url-pattern>/ProfileServlet</url-pattern>
</servlet-mapping>
<servlet>
<display-name>LogoutServlet</display-name>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/LogoutServlet</url-pattern>
</servlet-mapping>
</web-app>

servlate

_____________html________________
<forma ction="servlet1"method="post">
Name:<inputtype="text"name="userName"/>
<br/>
<inputtype="submit"value="go"/>
</form>

_________-servlate.java_______________
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
publicclassSecondServletextendsHttpServlet{
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
{
try
{
response.setContentType("text/html");
PrintWriterout=response.getWriter();
Cookieck[]=request.getCookies();
out.print("Hello"+ck[0].getValue());
out.print("This site is under Construction");
out.close();
}
catch
(Exceptione){System.out.println(e);
}
}
}
________________-web.xml_____________

<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
<servlet-name>s2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>