Practical 1
Practical 1
Practical 1
Sc IT ENTERPRISE JAVA
PRACTICALNO.1
Q.1 a) Create a simple calculator application using servlet.
CODE:
index.html
<html>
<body>
<form method=post action="CalcServlet">
NO-1 <input type=text name="t1">
NO-2 <input type=text name="t2"> <br> <br>
<input type=submit value="+" name="btn">
<input type=submit value="-" name="btn">
<input type=submit value="*" name="btn">
<input type=submit value="/" name="btn">
</form>
</body>
</html>
CalcServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CalcServlet extends HttpServlet
{ public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{ response.setContentType("text/html");
PrintWriter out = response.getWriter();
int a=Integer.parseInt(request.getParameter("t1"));
int b=Integer.parseInt(request.getParameter("t2")); int
c=0;
String op=request.getParameter("btn"); if
(op.equals("+"))
c=a+b;
else if (op.equals("-"))
c=a-b;
else if (op.equals("*"))
c=a*b;
else if (op.equals("/"))
c=a/b;
out.println("<b>"+a+op+b+" = "+c+"<b>"); }
}
OUTPUT:
Q.1 b) Create a servlet for a login page. If the username and password are correct
then it says message “Hello ” else a message “login failed”.
CODE:
index.html
<html>
<body>
<form action="LoginServlet" method="post">
UserName : <input type="text" name="uname"><br>
Password : <input type="password" name="pw"> <br>
<input type="submit" value="LOGIN">
</form>
</body>
</html>
LoginServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username=request.getParameter("uname");
String password=request.getParameter("pw");
String msg="";
if (username .equals("admin") && password.equals("admin123"))
msg="Hello "+username;
else
msg="Login failed";
out.println("<b>"+msg+"<b>");
}
}
OUTPUT:
Q.1 c) Create a registration servlet in Java using JDBC. Accept the details such as
Username, Password, Email, and Country from the user using HTML Form and store the
registration details in the database.
Code:
MySql Command from mysql software:-
1. Select services -> expand databases -> right click on MySQL server at
localhost:3306[disconnected] -> click on connect -> enter password (tiger) -> OK
2. Again right click on MySQL server at localhost:3306 -> select Create database ->
enter database name and select the check box to grant permission. 3. Right click on Table
under your daatbase
4. Enter table name user by replacing untitled. Click on Add column, name -> username, type-
> varchar, size-> 20, select checkbox of primary key, again click on Add column password
varchar size 20, again click on Add column emailid varchar(20), again click Add column
country varchar 10;
5. add mysql-connector to library folder of the current application
index.html
<html>
<body>
<form action="RegistrationServlet" method="post">
User name : <input type="text" name="uname"> <br>
Password : <input type="password" name="pw"><br>
Email Id : <input type="text" name="email"> <br>
Country : <select name="coun">
<option>select...
<option> India
<option> Bangladesh
<option> Bhutan
<option> Canada
</select> <br>
<input type="submit" value=" Register">
</form>
</body>
</html>
RegistrationServlet.java
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RegistrationServlet extends HttpServlet
{ public void doPost(HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException
{ Connection con=null;
PreparedStatement ps=null;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username=request.getParameter("uname");
String password=request.getParameter("pw");
String emailid=request.getParameter("email");
String country=request.getParameter("coun");
try
{ Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/registerdb","root","tiger");
out.println("connection done successfully...");
ps=con.prepareStatement("insert into user values (?,?,?,?)");
ps.setString(1,username);
ps.setString(2,password);
ps.setString(3,emailid);
ps.setString(4,country);
ps.execute();
out.print("Data insserted successfully!!!!");
}
catch(Exception e) { out.println(e); }
out.println("<b>"+"<b>");
}
}
OUTPUT: