Professional Documents
Culture Documents
Nishant AJ
Nishant AJ
HIET
PRACTICAL 1
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
Output:
PRACTICAL 2
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 {
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
Output:
PRACTICAL 3
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
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>
Output:
PRACTICAL 4
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
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>
Output:
PRACTICAL 5
AIM: WAP SERVLET Using Cookies to Remember User Preferences.
Index.html
<html>
<head>
<title>Get Method</title>
<meta charset="UTF-8">
</head>
<body>
<form action="MyServlet1">
</form>
</body>
MyServlet1.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
HttpServletResponse response) {
try{
response.setContentType("text/html");
pwriter.print("Hello "+name);
response.addCookie(c1);
response.addCookie(c2);
pwriter.close();
}catch(Exception exp){
System.out.println(exp);
MyServlet2.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
HttpServletResponse response){
try{
response.setContentType("text/html");
//Reading cookies
Cookie c[]=request.getCookies();
pwriter.print("Name: "+c[1].getValue());
pwriter.print("Password: "+c[2].getValue());
pwriter.close();
}catch(Exception exp){
System.out.println(exp);
Output:
PRACTICAL 6
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>
package roseindia;
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
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
);
Now let's start creating this app. Use the following procedure to do that in the NetBeans
IDE.
Step 1.
Step 2.
Step 3.
Step 4.
SwingSearchApp.java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
//Initializing Components
JLabel lb, lb1, lb2, lb3, lb4, lb5;
JTextField tf1, tf2, tf3, tf4, tf5;
JButton btn;
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
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);
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);
System.out.println(ex);
}
}
//Running Constructor
Step 5
Now our project is ready to run. Right-click on the project menu then choose "Run". The
following output is generated.
Step 6
Case 1:
Note:
We use this dialogue box for generating an error message. So a new dialogue box appears with the
message.
Case 2:
Case 3:
PRACTICAL 8
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
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: