Lab Program1
Lab Program1
CODE:
index.html
<html>
<body>
</form>
</body>
</html>
CalcServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
{ response.setContentType("text/html");
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>
</body>
</html>
LoginServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
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:
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:
index.html
<html>
<body>
<option>select...
<option> India
<option> Bangladesh
<option> Bhutan
<option> Canada
</select> <br>
</form>
</body>
</html>
RegistrationServlet.java
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
{ Connection con=null;
PreparedStatement ps=null;
response.setContentType("text/html");
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: