KEMBAR78
Java 1 | PDF | Web Server | Internet & Web
0% found this document useful (0 votes)
11 views22 pages

Java 1

The document outlines a series of practical projects related to web server configuration and Java web development using Servlets and JSP. It includes steps for installing Apache Tomcat, creating a registration page with validation, and various JSP examples demonstrating string display, arithmetic operations, and using JSP action tags. Each project is accompanied by code snippets and explanations to guide the implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views22 pages

Java 1

The document outlines a series of practical projects related to web server configuration and Java web development using Servlets and JSP. It includes steps for installing Apache Tomcat, creating a registration page with validation, and various JSP examples demonstrating string display, arithmetic operations, and using JSP action tags. Each project is accompanied by code snippets and explanations to guide the implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

INDEX

Serial
Objective of Projects Remark Date
NO.
To install and configration of web server
1
Tomcat.
Develop a registration page in servlet with
2 proper validation and store all records in
the database.
Write a program in servlet to display Hello
3
World on the browser.

4 Write a program in jsp to display a string.

Write a program in jsp to find sum of two


5
numbers using scriptlet tag.

Write a program in jsp to find sum of two


6
numbers using jsp delclaration.
Write a program in jsp to print the current
7 date using jsp page directive (include
directive).
Write a program in jsp to print a message
8 with the help of jsp action tag
(jsp:forward).
Write a program in jsp to print a message
9
with the help of jsp action tag(jsp:include).

Write a program in jsp to print a message


10
with the help of jsp action tag(jsp:param).
Practical No. : 01
Object:- To Install and configuration Web Server Tomcat.
Theory:- The Apache Tomcat software is an open source implementation of the java Servlet, JavaServer Pages,
Java Expression Language and Java WebSocket Technologies. Apache Tomcat is usually used as a Servlet
Container even though Tomcat has a fully functional HTTP Server to serve static content. In most of production,
Tomcat is used in conjunction with Apache HTTP Server where Apache HTTP Server attends static content like
html, images etc., and forwards the requests for dynamic content to Tomcat. This is because Apache HTTP
Server supports more advanced options than that of Tomcat.Latest Apache Tomcat version 8.5 adds support for
HTTP/2, OpenSSL for JSSE, TLS virtual hosting and JASPIC 1.1

Prerequisites:-

JDK or JRE will need to be installed on the Windows Server before you can configure Tomcat 9 on the server.
OpenJDK and Amazon Corretto are two examples of open-source Java Development Kit providers.

Installing Tomcat 9:-

Step 1. Open the Google Chrome or any of your web browser and type "download Tomcat for windows" in the
search box. You can also go directly on Tomcat's website by clicking on
this https://tomcat.apache.org/download-90.cgi#9.0.38
Now download any version of Tomcat you want:
Step 2. Go to Download and click on the downloaded file and wait for little until the installation process starts.

Step 3. Once the installation process gets started, click on the "Next" button, as shown below:

Step 4. Click on the button labeled as "I Agree."


Step 5. Click on the "Next" button.

Step 6. Enter the user name and password and click on the " Next" button, as shown below:
Step 7. Then click on the "Next" button again

Step 8. Now click on the "Install" button.


Wait for some time until the Tomcat gets installed.

Step 9. Now click on the "Finish" button, here the installation of Tomcat is completed. It may ask you to restart
your system, so restart your system.
Now you can start the Tomcat by clicking on its icon, and you can start and stop the server.
Practical-02

Object:- Develop a registration page in servlet with proper validation and store all
records in the database.

Answer:

In the below example we take all detail from the user and store them on to the database. If user leaves any of
the fields blank, then it will not be saved.

Create a "Registration" table

SQL> create table registration (Name varchar2(20), password varchar2(10), email varchar2(20), mobile number,
dob varchar2(20), gender varchar2(6), country varchar2 (10));

There are three files in registration page

i. register.html
ii. Register.java
iii. web.xml

register.html

<!doctype html>
<body>
<form action="servlet/Register" method="post">
<fieldset style="width:20%; background-color:#ccffeb">
<h2 align="center">Registration form</h2><hr>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="userName" required /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="userPass" required /> </td>
</tr>
<tr>
<td>Email Id</td>
<td><input type="text" name="userEmail" required /></td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="userMobile" required/></td>
</tr>
<tr>
<td>Date of Birth</td>
<td><input type="date" name="userDOB" required/></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="gender" value="male" checked> Male
<input type="radio" name="gender" value="female"> Female </td></tr>
<tr>
<td>Country</td>
<td><select name="userCountry" style="width:130px">
<option>Select a country</option>
<option>India</option>
<option>America</option>
<option>England</option>
<option>other</option></select>
</td>
</tr>
<tr>
<td><input type="reset" value="Reset"/></td>
<td><input type="submit" value="Register"/></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>

Register.java

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

public class Register extends HttpServlet


{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

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


String pwd = request.getParameter("userPass");
String email = request.getParameter("userEmail");
int mobile = Integer.parseInt(request.getParameter("userMobile"));
String dob = request.getParameter("userDOB");
String gender = request.getParameter("gender");
String country =request.getParameter("userCountry");
try
{
//load the driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//create connection object
Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","local","test");
// create the prepared statement object
PreparedStatement ps=con.prepareStatement("insert into registration values(?,?,?,?,?,?,?)");
ps.setString(1,name);
ps.setString(2,pwd);
ps.setString(3,email);
ps.setInt(4, mobile);
ps.setString(5,dob);
ps.setString(6,gender);
ps.setString(7,country);
int i = ps.executeUpdate();
if(i>0)
out.print("You are successfully registered...");
}
catch (Exception ex)
{
ex.printStackTrace();
}
out.close();
}
}
web.xml

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

Output:-

This page doesn’t allow to submit the record without filling the textbox.

After filling all textbox, it allows to submit the record.


Click on Register button.
Practical-03

Object:- Write a program in servlet to display Hello World on browser.

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* This servlet program is used to print "Hello World" on
* client browser by implementing servlet interface.
* @author w3spoint
*/
public class HelloWorld implements Servlet {
private static final long serialVersionUID = 1L;
//no-argument constructor.
public HelloWorld() {
}
ServletConfig config=null;
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
System.out.println("Do initialization here.");
}
@Override
public void destroy() {
System.out.println("Do clean-up process here.");
}
@Override
public ServletConfig getServletConfig() {
return config;
}
@Override
public String getServletInfo() {
return "w3spoint.com";
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Hello World example using " +
"servlet interface.</h1>");
out.close();
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>
com.w3spoint.business.HelloWorld
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
Output:
Practical - 04

Object:- Write a program in jsp to display a string.


String.jsp
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Hello World.</h1>
</body>
</html>
web.xml
<web-app>
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
Output:
Practical-05

Object:- Write a program in jsp to find sum of two numbers using scriptlet tag.

welcome.jsp

<html>
<head>
<title>Scriplet tag example</title>
</head>
<body>
<%
out.println("Sum of 10 and 20 = ");
out.print(10 + 20);
%>
</body>
</html>
web.xml
<web-app>
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
Output:
Practical-06

Object:- Write a program in jsp to find sum of two numbers using jsp delclaration.
welcome.jsp

<html>
<head>
<title>Declaration tag example</title>
</head>
<body>
<%!
int sum(int a, int b){
return a + b;
}
%>
<%
out.println("Sum of 10 and 20 = " + sum(10, 20));
%>
</body>
</html>
web.xml
<web-app>

<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>

</web-app>
Output:
Practical-07

Object:- Write a program in jsp to print the current date using jsp page directive (include
directive).
Import Attribute Of JSP Page Directive

welcome.jsp:
<%@ page import="java.util.Date" %>

<html>
<head>
<title>import page directive example</title>
</head>
<body>
Current date: <%= new Date() %>
</body>
</html>

web.xml:
<web-app>

<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>

</web-app>

Output:
Practical-08
Object:- Write a program in jsp to print a message with the help of jsp action tag
(jsp:forward).

welcome.jsp

<html>
<head>
<title>forward action example</title>
</head>
<body>
<jsp:forward page="home.jsp"/>
</body>
</html>

home.jsp

<html>
<head>
<title>forward action example</title>
</head>
<body>
<h4>This content is of that resource
on which request is forwarded.</h4>
</body>
</html>

web.xml

<web-app>

<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>

</web-app>
Output:
Practical-09

Object:- Write a program in jsp to print a message with the help of jsp action
tag(jsp:include).

welcome.jsp

<html>
<head>
<title>include action example</title>
</head>
<body>
<h3>Hello this is a include action example.</h3>
<jsp:include page="home.jsp"/>
</body>
</html>

home.jsp

<html>
<head>
<title>include action example</title>
</head>
<body>
<h4>This content is of included file.</h4>
</body>
</html>

web.xml

<web-app>

<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>

</web-app>
Output:
Practical-10

Object:- Write a program in jsp to print a message with the help of jsp action
tag(jsp:param)

welcome.jsp

<html>
<head>
<title>param action example</title>
</head>
<body>
<h3>Hello this is a param action example.</h3>
<jsp:forward page="home.jsp">
<jsp:param name="websiteName" value="www.gliagra.ac.in"/>
</jsp:forward>
</body>
</html>

home.jsp

<html>
<head>
<title>param action example</title>
</head>
<body>
<h4>
This content is of that resource on which request is forwarded.
</h4>
<%
out.print("Website: " + request.getParameter("websiteName"));
%>
</body>
</html>

web.xml:

<web-app>

<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>

</web-app>
Output:

You might also like