Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

1

HIET

PRACTICAL 1

AIM: WAP JAVA Servlet Program to implement a dynamic HTML using


Servlet (user name and password should be accepted using HTML
and displayed using a Servlet).

Login.java File

import javax.servlet.http.*;
import java.io.*;
public class Login extends HttpServlet
{ public void doPost(HttpServletRequest request,HttpServletResponse response)
{try{
String username=request.getParameter("username");
String pass=request.getParameter("pass");
response.setContentType("text/html");
PrintWriter writer =response.getWriter();
writer.println("<html><body>");
writer.println("<h4> Thank You"+username+"your password is "+pass+"for log in");
writer.println("<br> <br> <b> UserName:"+username);
writer.println("<br> <br> <b> password:"+pass);
writer.println("</body><html>");
writer.close();
}
catch(Exception e)
{
e.printStackTrace();}}}

Login.html File

<HTML>
<Head>
<Title> Login Page </Title>
</Head>
<Body>
<Center>
<H1> Login Page</H1>
</Center>
<B> Please Enter Your User Name and Password
ADVANCE JAVA LAB NISHANT
SHARMA(1601915011)
2
HIET

<Form action="http://localhost:8080/examples/servlet/Login" method=Post>


<p> <b> Username<input type="text" name="username" length=40>
<p> <b> Password <input type="text" name="pass" length=30>
<p> <input type="submit" value="submit">
</Form>
</Body></HTML>

Output:

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
3
HIET

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
4
HIET

PRACTICAL 2

AIM: Write a JAVA Servlet Program to Auto Web Page Refresh


(Consider a webpage which is displaying Date and time or stock
market status. For all such type of pages, you would need to refresh
your web page regularly; Java Servlet makes this job easy by
providing refresh automatically after a given interval).

index.html

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>JAVA Servlet Program to Auto Web Page Refresh</p>
<p>Click on the following link for Auto Web Page Refresh:</p>
<!-- this will redirect to ServletApplication as URL-pattern for ServletApplication was
mentioned as /home -->
<a href="home"><b>More...</b></a> <hr />
</body>
</html>

TestServlet.java

import java.io.*;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
5
HIET

response.setContentType("text/html");
response.addHeader("Refresh", "2");
out.println("TestServlet says hi at " + new Date());
} finally {
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>


<web-app>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
6
HIET

Output:

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
7
HIET

PRACTICAL 3

AIM: WAP a java servlet to implement and demonstrate get() method.

HelloForm.html

<html>
<body>
<form action = "HelloForm" method = "GET">
First Name: <input type = "text" name = "first_name">
<br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>
</body>
</html>

HelloForm.java

// Import required java libraries


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class


public class HelloForm extends HttpServlet {
// Method to handle GET method request.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Using GET Method to Read Form Data";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<ul>\n" +
ADVANCE JAVA LAB NISHANT
SHARMA(1601915011)
8
HIET

" <li><b>First Name</b>: "


+ request.getParameter("first_name") + "\n" +
" <li><b>Last Name</b>: "
+ request.getParameter("last_name") + "\n" +
"</ul>\n" +
"</body>
</html>"
);
}

// Method to handle POST method request.


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
}
}

Web.xml

<servlet>
<servlet-name>HelloForm</servlet-name>
<servlet-class>HelloForm</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloForm</servlet-name>
<url-pattern>/HelloForm</url-pattern>
</servlet-mapping>

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
9
HIET

Output:

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
10
HIET

PRACTICAL 4

AIM: WAP a java servlet to implement and demonstrate post() method.

HelloForm.html

<html>
<body>
<form action = "HelloForm" method = "POST">
First Name: <input type = "text" name = "first_name">
<br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>
</body>
</html>

HelloForm.java

// Import required java libraries


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class


public class HelloForm extends HttpServlet {

// Method to handle GET method request.


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// Set response content type


response.setContentType("text/html");

PrintWriter out = response.getWriter();


String title = "Using GET Method to Read Form Data";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";

out.println(docType +
ADVANCE JAVA LAB NISHANT
SHARMA(1601915011)
11
HIET

"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>First Name</b>: "
+ request.getParameter("first_name") + "\n" +
" <li><b>Last Name</b>: "
+ request.getParameter("last_name") + "\n" +
"</ul>\n" +
"</body>
</html>"
);
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
}
}

Web.xml

<servlet>
<servlet-name>HelloForm</servlet-name>
<servlet-class>HelloForm</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloForm</servlet-name>
<url-pattern>/HelloForm</url-pattern>
</servlet-mapping>

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
12
HIET

Output:

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
13
HIET

PRACTICAL 5
AIM: WAP SERVLET Using Cookies to Remember User Preferences.
Index.html

<html>

<head>

<title>Get Method</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width">

</head>

<body>

<form action="MyServlet1">

User Name:<input type="text" name="userName"/><br/>

Password:<input type="password" name="userPassword"/><br/>

<input type="submit" value="submit"/>

</form>

</body>

MyServlet1.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class MyServlet1 extends HttpServlet

public void doGet(HttpServletRequest request,

HttpServletResponse response) {

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
14
HIET

try{

response.setContentType("text/html");

PrintWriter pwriter = response.getWriter();

String name = request.getParameter("userName");

String password = request.getParameter("userPassword");

pwriter.print("Hello "+name);

pwriter.print("Your Password is: "+password);

//Creating two cookies

Cookie c1=new Cookie("userName",name);

Cookie c2=new Cookie("userPassword",password);

//Adding the cookies to response header

response.addCookie(c1);

response.addCookie(c2);

pwriter.print("<br><a href='MyServlet2'>View Details</a>");

pwriter.close();

}catch(Exception exp){

System.out.println(exp);

MyServlet2.java

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
15
HIET

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class MyServlet2 extends HttpServlet {

public void doGet(HttpServletRequest request,

HttpServletResponse response){

try{

response.setContentType("text/html");

PrintWriter pwriter = response.getWriter();

//Reading cookies

Cookie c[]=request.getCookies();

//Displaying User name value from cookie

pwriter.print("Name: "+c[1].getValue());

//Displaying user password value from cookie

pwriter.print("Password: "+c[2].getValue());

pwriter.close();

}catch(Exception exp){

System.out.println(exp);

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
16
HIET

Output:

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
17
HIET

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
18
HIET

PRACTICAL 6

AIM: Write a JSP program to demonstrate the import attribute.

This practical shows you how to import a java package or the class in your jsp
application. Here a Java code has also been provided which class has been imported in
the following JSP code like <%@page import="roseindia.Extends" %> in which, the
import is the attribute of the page directive in JSP and the value of the attribute is the
"roseindia.Extends". Here, roseindia is the package name and the Extends is the class
which is made after compilation of the Extends.java file. This class file is contained by
the folder which determines the package name. And the package name folder is putted
in the classes folder inside the <your application root directory>/WEB-
INF/classes/package_name.
Basically, this attribute of the page directive imports the java packages and it's classes
more and more. You can import more than one java packages and classes by
separating with comma (,). You can set the name of the class with the package name
directly like packagename.classname or import all classes of the package by using
packagename.*.
Here is the code of the program:
<%@page import="roseindia.Extends" %><html>
<head><title>Example of Extends Attribute of page
Directive in JSP</title></head> <body> <font
size="20" color="red">
<%
Extends ex = new Extends();
out.print(ex.show());
%>
</font>
</body>
</html>

Here is the code of Extends.java file:

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
19
HIET

package roseindia;

public class Extends{


public String show(){
return "Roseindia.net";
}
}

Output of the Jsp program:

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
20
HIET

PRACTICAL 7
AIM: Write an application to search Database using GUI Swing.

This article explains how to search user records from a database by their name in the
Swing GUI of Java. The NetBeans IDE is used to create the sample examples.

Searching Records from Database in a Windows Forms form using Swing GUI

For creating this app, we need the following files:

1. Java file (SwingSearchApp.java)


2. SQL table (emp.sql)

1. SwingSearchApp.java

This Java file consists of the entire logic. First of all we initialize the JFrame components
using a constructor then create a database connection and finally set the database
value to the textfield. If the given name is not found in the database then it displays an
error message and displays it by running the constructor.

2. emp.sql

For fetching records we need a database table; for that we create an "emp" table in our
database.

Syntax-

emp.sql

create table emp

uname varchar2(20), umail varchar2(30),

upass varchar2(20), ucountry varchar2(20)

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
21
HIET

);

Insert some rows

The following SQL will insert some rows in it:

1. insert into emp values ('sandeep', 'sandy05.1991@gmail.com', 'welcome', 'India');

2. insert into emp values ('rahul', 'rahul@gmail.com' , '123', 'India');

Now let's start creating this app. Use the following procedure to do that in the NetBeans
IDE.

Step 1.

Open the NetBeans IDE.

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
22
HIET

Step 2.

Choose "Java" -> "Java application" as shown below.

Step 3.

Type your project name as "SwingSearchApp" as in the following.

Step 4.

Now write the following code in the "SwingSearchApp.java" file.

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
23
HIET

SwingSearchApp.java

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;

public class SwingSearchApp extends JFrame implements ActionListener {

//Initializing Components
JLabel lb, lb1, lb2, lb3, lb4, lb5;
JTextField tf1, tf2, tf3, tf4, tf5;
JButton btn;

//Creating Constructor for initializing JFrame components


SwingSearchApp() {
//Providing Title
super("Fetching Student Information");
lb5 = new JLabel("Enter Name:");
lb5.setBounds(20, 20, 100, 20);
tf5 = new JTextField(20);
tf5.setBounds(130, 20, 200, 20);

btn = new JButton("Submit");


btn.setBounds(50, 50, 100, 20);
btn.addActionListener(this);

lb = new JLabel("Fetching Student Information From Database");


lb.setBounds(30, 80, 450, 30);
lb.setForeground(Color.red);
lb.setFont(new Font("Serif", Font.BOLD, 20));

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
24
HIET

setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);

lb1 = new JLabel("U_Name:");


lb1.setBounds(20, 120, 100, 20);
tf1 = new JTextField(50);
tf1.setBounds(130, 120, 200, 20);
lb2 = new JLabel("U_Mail:");
lb2.setBounds(20, 150, 100, 20);
tf2 = new JTextField(100);
tf2.setBounds(130, 150, 200, 20);
lb3 = new JLabel("U_Pass:");
lb3.setBounds(20, 180, 100, 20);
tf3 = new JTextField(50);
tf3.setBounds(130, 180, 200, 20);
lb4 = new JLabel("U_Country:");
lb4.setBounds(20, 210, 100, 20);
tf4 = new JTextField(50);
tf4.setBounds(130, 210, 100, 20);
setLayout(null);

//Add components to the JFrame


add(lb5);
add(tf5);
add(btn);

add(lb);
add(lb1);
add(tf1);
add(lb2);
ADVANCE JAVA LAB NISHANT
SHARMA(1601915011)
25
HIET

add(tf2);
add(lb3);
add(tf3);
add(lb4);
add(tf4);

//Set TextField Editable False


tf1.setEditable(false);
tf2.setEditable(false);
tf3.setEditable(false);
tf4.setEditable(false);
}

public void actionPerformed(ActionEvent e) {


//Create DataBase Coonection and Fetching Records

try {
String str = tf5.getText();

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521", "sandeep",
"welcome");
PreparedStatement st = con.prepareStatement("select * from emp where
uname=?");
st.setString(1, str);

//Excuting Query
ResultSet rs = st.executeQuery();

if (rs.next()) {
ADVANCE JAVA LAB NISHANT
SHARMA(1601915011)
26
HIET

String s = rs.getString(1);
String s1 = rs.getString(2);
String s2 = rs.getString(3);
String s3 = rs.getString(4);

//Sets Records in TextFields.


tf1.setText(s);
tf2.setText(s1);
tf3.setText(s2);
tf4.setText(s3);
} else {
JOptionPane.showMessageDialog(null, "Name not Found");
}

//Create Exception Handler


} catch (Exception ex) {

System.out.println(ex);
}
}
//Running Constructor

public static void main(String args[]) {


new SwingSearchApp();
}
}

Step 5

Now our project is ready to run. Right-click on the project menu then choose "Run". The
following output is generated.

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
27
HIET

Step 6

Now type the names of students to search for.

Case 1:

Enter an incorrect name as in the following and then click on "Submit".

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
28
HIET

Note:

JOptionPane.showMessageDialog(null, "Name not Found");

We use this dialogue box for generating an error message. So a new dialogue box appears with the
message.

Case 2:

Searching records for "rahul".

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
29
HIET

Case 3:

Searching records for "sandeep".

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
30
HIET

PRACTICAL 8

AIM: Write a JAVA JSP Program to implement verification of a


particular user login and display a Welcome page.

login.jsp

<html>
<head> <title> Login page </title>
</head>
<body>
<form action="validation.jsp">
<table border="0">
<tr>
<td> USER ID: </td>
<td>
<input type="text" name="uname" /> <br>
</td>
</tr>
<tr>
<td> PASSWORD: </td>
<td>
<input type="password" name="password" /> <br>
</td>
</tr>
<tr> <td align ="center">
<input type="submit" value="submit" >
<input type="reset" value="reset">
</td>
</tr>
</form>
</body>
</html>

validation.jsp

<html>
<body>
<%! String uid="student"; %>
ADVANCE JAVA LAB NISHANT
SHARMA(1601915011)
31
HIET

<%! String pass="aitmca"; %>


<%! String id, password; %>
<% id=request.getParameter("uname"); %>
<% password=request.getParameter("password"); %>
<% if(uid.equals(id)&&pass.equals(password))
{
%>
<jsp:forward page="welcome.jsp"/>
<%
}
else
%>
<jsp:forward page="error.jsp" />
<%
}
%>
</body>
</html>

welcome.jsp

<html>
<body bgcolor="yellow">
<center>
<%! String id; %>
<% id=request.getParameter("uname"); %>
<h1> welcome <%=id%> to the home page </h1>
</center>
</body>
</html>

error.jsp

<html>
<head> <title>JSP Page</title> </head>
<body bgcolor="pink">
<h1>INVALID ENTRY</h1>
</body>
</html>
ADVANCE JAVA LAB NISHANT
SHARMA(1601915011)
32
HIET

Output:

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)
33
HIET

ADVANCE JAVA LAB NISHANT


SHARMA(1601915011)

You might also like