You are on page 1of 14

CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

Ex.No:10

CONVERT THE STATIC WEBPAGES OF PROGRAMS INTO DYNAMIC WEB


PAGES USING SERVLETS AND COOKIES

Aim: To Install TOMCAT web server. Convert the static WebPages of programs 1&2 into
dynamic WebPages using servlets (or JSP) and cookies. Hint: Users information (user id,
password, credit card number) would be stored in web.xml. Each user should have a separate
Shopping Cart.

Description: Apache Tomcat is an open source software implementation of the Java Servlet
and JavaServer Pages technologies. The Java Servlet and JavaServer Pages specifications are
developed under the Java Community Process. Apache Tomcat is developed in an open and
participatory environment and released under the Apache License version 2. Apache Tomcat
is intended to be a collaboration of the best-of-breed developers from around the world. We
invite you to participate in this open development project. To learn more about getting
involved, click here. Apache Tomcat powers numerous large-scale, mission-critical web
applications across a diverse range of industries and organizations.

Procedure:

Apache Tomcat is a opensource webserver product of Apache Foundation like Apache


HTTP server. It is used to deploying Java Servlet and JSP applications. To deploy any
application in Tomcat we can simply create a war file and deploy them. For more details
about you can visit apache official site http://tomcat.apache.org/ .

To install Tomcat 7 on CentOS/RHEL servers. We are using CentOS 7 and installing


Apache tomcat 7.0.63.

Step 1: Check Java Version

JAVA is the first requirement for Tomcat 7 installation. Make sure you have JAVA SE 6 or
Later version installed in your system. Use following command to check if you have java
installed already on your system.

# java -version

java version "1.8.0_31"


Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

Step 2: Downloading Tomcat 7 Archive

Download Apache Tomcat archive file from Apache tomcat official download page. You can
use below wget command to download it.

# cd /tmp
# wget http://www.us.apache.org/dist/tomcat/tomcat-7/v7.0.63/bin/apache-
tomcat-7.0.63.tar.gz

Sample Output

Using CATALINA_BASE: /usr/local/tomcat7


Using CATALINA_HOME: /usr/local/tomcat7

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

Using CATALINA_TMPDIR: /usr/local/tomcat7/temp


Using JRE_HOME: /opt/jdk1.8.0_31
Using CLASSPATH:
/usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Tomcat started.

Step 4: Access Tomcat in Browser

Tomcat server works on port 8080 default. To access Tomcat on web browser by connecting
your server on port 8080.

http://svr1.tecadmin.net:8080

Step 5: Setup User Accounts

Finally we need to create user accounts to secure and access admin/manager pages. Edit
conf/tomcat-users.xml file in your editor and paste inside <tomcat-users> </tomcat-users>
tags.

<!-- user manager can access only manager section -->


<role rolename="manager-gui" />
<user username="manager" password="_SECRET_PASSWORD_" roles="manager-gui"
/>

<!-- user admin can access manager and admin section both -->
<role rolename="admin-gui" />
<user username="admin" password="_SECRET_PASSWORD_" roles="manager-
gui,admin-gui" />

Step 6: Stop Tomcat

Finally if you feel that there are no need of Tomcat in your system, You can simply stop it
using below command from tomcat home directory.

# ./bin/shutdown.sh

Program:

Index.html
<html xmlns="http://www.w3.org/1999/xhtml">
<body bgcolor="pink">
<br /><br /><br /><br /><br />
<h1 align="center"><U>ONLINE BOOK STORAGE</U></h1><br /><br /><br />
<h2 align="center"><pre>

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

<b>Welcome to online book storage.


Press LOGIN if you are having id
otherwise press REGISTRATION
</b></pre></h2>
<br /><br /><pre>
<div align="center"><a href="Login.html">LOGIN</a> <a href="reg.html">
REGISTRATION</a></div></pre>
</body></html>
Login.html
<html>
<body bgcolor="blue"><br /><br /><br />
<form name="myform" method="post" action="login">
<div align="center"><pre>
LOGIN ID :<input type="text" name="id" /><br />
PASSWORD :<input type="password" name="pwd" /></pre><br /><br />
</div>
<br /><br />
<div align="center">
<input type="submit" value="ok" onclick="validate()" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="clear" />
</div></form></body></html>
Profile.html
<html xmlns="http://www.w3.org/1999/xhtml">
<body bgcolor="yellow"><br /><br /><br />
<form name="myform" method="post" action="profile">
<div align="center"><pre>
LOGIN ID :<input type="text" name="id" /><br />
</pre><br /><br />
</div>
<br /><br />
<div align="center">
<input type="submit" value="ok" onclick="validate()" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="clear" />
</div></form></body></html>
Reg.html
<html xmlns="http://www.w3.org/1999/xhtml">
<body bgcolor="red"><br /><br />
<form name="myform" method="post" action="reg">
<div align="center"><pre>
NAME :<input type="text" name="name" /><br />
ADDRESS :<input type="text" name="addr" /><br />
CONTACT NUMBER :<input type="text" name="phno" /><br />
LOGINID :<input type="text" name="id" /><br />
PASSWORD :<input type="password" name="pwd" /></pre><br /><br />
</div>
<br /><br />
<div align="center">
<input type="submit" value="ok" onclick="validate()" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="clear" />
</div></form></body></html>
Catlog.html
<html xmlns="http://www.w3.org/1999/xhtml">

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

<body bgcolor="white"><br /><br /><br />


<form method="post" action="catalog">
<div align="center"><pre>
BOOK TITLE :<input type="text" name="title" /><br />
</pre><br /><br />
</div>
<br /><br />
<div align="center">
<input type="submit" value="ok"
name="button1"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="clear" name="button2"/>
</div></form></body></html>
Order.html
<html xmlns="http://www.w3.org/1999/xhtml">
<body bgcolor="green"><br /><br />
<form method="post" action="reg">
<div align="center"><pre>
NAME :<input type="text" name="name" /><br />
PASSWORD :<input type="password" name="pwd" />
TITLE :<input type="text" name="title" /><br />
NO. OF BOOKS :<input type="text" name="no" /><br />
DATE :<input type="text" name="date" /><br />
CREDIT CARD NUMBER:<input type="password" name="cno" /><br /></pre><br /><br
/>
</div>
<br /><br />
<div align="center">
<input type="submit" value="ok" name="button1"/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="clear"
name="button2"/>
</div></form></body></html>
Login.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class login extends HttpServlet
{
@Override
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{ PrintWriter pw=resp.getWriter();
pw.println("<html><body bgcolor=green>");
String id=req.getParameter("id");
String pwd=req.getParameter("pwd");

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection
con=DriverManager.getConnection("jdbc:derby://localhost:1527/OnlineBookStorage","root",
"root");
Statement stmt=con.createStatement();
String sqlstmt="select id,password from login";
ResultSet rs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.next())
{
if(id == null ? rs.getString("name") == null : id.equals(rs.getString("name")))
{
flag=1;
}
if(flag==0)
{
pw.println("SORRY INVALID ID TRY AGAIN ID<br><br>");
pw.println("<a href=\"login.html\">press LOGIN to RETRY</a>");
}
else
{
pw.println("VALID LOGIN ID<br><br>");
pw.println("<h3><ul>");
pw.println("<li><ahref=\"profile.html\"><fontcolor=\"black\">USER PROFILE</font>
</a></li><br><br>");
pw.println("<li><ahref=\"catalog.html\"><fontcolor=\"black\">BOOKS
CATALOG</font></a></li><br><br>");
pw.println("<li><ahref=\"order.html\"><fontcolor=\"black\">ORDER
CONFIRMATION</font> </a></li><br><br>");
}
pw.println("</body></html>");
}}
catch(ClassNotFoundException | SQLException e)
{ resp.sendError(500,e.toString());
}
}
}
Reg.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class reg extends HttpServlet

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

{
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
PrintWriter pw=resp.getWriter();
pw.println("<html><body bgcolor=pink>");
String name=req.getParameter("name");
String addr=req.getParameter("addr");
String phno=req.getParameter("phno");
String id=req.getParameter("id");
String pwd=req.getParameter("pwd");
int no=Integer.parseInt(phno);
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection
con=DriverManager.getConnection("jdbc:derby://localhost:1527/OnlineBookStorage","root",
"root");
Statement stmt=con.createStatement();
String sqlstmt="select id,password from login";
ResultSet rs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.next())
{
if(rs.getString("name") == null ? id == null : rs.getString("name").equals(id))
{
pw.println("SORRY INVALID ID ALREADY EXITS TRY AGAIN WITH NEW
ID<br><br>");
pw.println("<a href=\"/tr/reg.html\">press REGISTER to RETRY</a>");
}
else
{ Statement stmt1=con.createStatement();
stmt1.executeUpdate("insert into login
values("+name+","+addr+","+no+","+id+","+pwd+")");
pw.println("YOUR DETAILS ARE ENTERED<br><br>");
pw.println("<a href=\"login.html\">press LOGIN to login</a>");
}
pw.println("</body></html>");
}
}
catch(ClassNotFoundException | SQLException e)
{ resp.sendError(500,e.toString());
} }}
Catlog.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Catlog extends HttpServlet
{
@Override
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
PrintWriter pw=resp.getWriter();
pw.println("<html><body bgcolor=green>");
String title=req.getParameter("title");
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection
con=DriverManager.getConnection("jdbc:derby://localhost:1527/OnlineBookStorage","root",
"root");
Statement stmt=con.createStatement();
String sqlstmt="select id,password from login";
ResultSet rs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.next())
{
pw.println(",div align=\"center\">");
pw.println("TITLE :"+rs.getString(1)+"<br>");
pw.println("AUTHOR :"+rs.getString(2)+"<br>");
pw.println("VERSION :"+rs.getString(3)+"<br>");
pw.println("PUBLISHER :"+rs.getString(4)+"<br>");
pw.println("COST :"+rs.getString(5)+"<br>");
pw.println("</div");
flag=1;
}
if(flag==0)
{
pw.println("SORRY INVALID TITLE TRY AGAIN <br><br>");
pw.println("<a href=\"/tr/catalog.html\">press HERE to RETRY</a>");
}
pw.println("</body></html>");
}
catch(ClassNotFoundException | SQLException e)
{
resp.sendError(500,e.toString());
}
}
}
Profile.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Profile extends HttpServlet
{
@Override
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
PrintWriter pw=resp.getWriter();
pw.println("<html><body bgcolor=green>");
String id=req.getParameter("id");
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection
con=DriverManager.getConnection("jdbc:derby://localhost:1527/OnlineBookStorage","root",
"root");
Statement stmt=con.createStatement();
String sqlstmt="select * from login where id="+id+"";
ResultSet rs=stmt.executeQuery(sqlstmt);
int flag=0;
pw.println("<br><br><br>");
while(rs.next())
{
pw.println("<div align=\"center\">");
pw.println("NAME :"+rs.getString(1)+"<br>");
pw.println("ADDRESS :"+rs.getString(2)+"<br>");
pw.println("PHONE NO :"+rs.getString(3)+"<br>");
pw.println("</div>");
flag=1;
}
if(flag==0)
{
pw.println("SORRY INVALID ID TRY AGAIN ID<br><br>");
pw.println("<a href=\"/tr/profile.html\">press HERE to RETRY</a>");
}
pw.println("</body></html>");
}
catch(Exception e)
{
resp.sendError(500,e.toString());
}
}
}
Order.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class order extends HttpServlet
{
@Override
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
PrintWriter pw=resp.getWriter();
pw.println("<html><body bgcolor=green>");
String id=req.getParameter("id");
String pwd=req.getParameter("pwd");
String title=req.getParameter("title");
String count1=req.getParameter("no");
String date=req.getParameter("date");
String cno=req.getParameter("cno");
int count=Integer.parseInt(count1);
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
try (Connection con =
DriverManager.getConnection("jdbc:derby://localhost:1527/OnlineBookStorage","root","root
")) {
Statement stmt=con.createStatement();
String sqlstmt="select id,password from login";
ResultSet rs=stmt.executeQuery(sqlstmt);
int flag=0,amount,x;
while(rs.next())
{
if(id.equals(rs.getString(1))&&pwd.equals(rs.getString(2)))
{
flag=1;
}
} if(flag==0)
{
pw.println("SORRY INVALID ID TRY AGAIN ID<br><br>");
pw.println("<a href=\\order.html\\>press HERE to RETRY</a>");
}
else
{
Statement stmt2=con.createStatement();
String s="select cost from book where title="+title+"";
ResultSet rs1=stmt2.executeQuery(s);
int flag1=0;
while(rs1.next())
{
flag1=1;

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

x=Integer.parseInt(rs1.getString(1));
amount=count*x;
pw.println("AMOUNT :"+amount+"<br><br><br><br>");
Statement stmt1=con.createStatement();
stmt1.executeUpdate("insert into details
values("+id+","+title+","+amount+","+cno+")");
pw.println("YOUR ORDER has taken<br>");
}
if(flag1==0)
{
pw.println("SORRY INVALID ID TRY AGAIN ID<br><br>");
pw.println("<a href=\\order.html\\>press HERE to RETRY</a>");
}
} pw.println("</body></html>");
}
}
catch(ClassNotFoundException | SQLException | NumberFormatException e)
{
resp.sendError(500,e.toString());
}
}}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>login</servlet-class>
</servlet>
<servlet>
<servlet-name>reg</servlet-name>
<servlet-class>reg</servlet-class>
</servlet>
<servlet>
<servlet-name>Catlog</servlet-name>
<servlet-class>Catlog</servlet-class>
</servlet>
<servlet>
<servlet-name>Profile</servlet-name>
<servlet-class>Profile</servlet-class>
</servlet>
<servlet>
<servlet-name>order</servlet-name>
<servlet-class>order</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

<servlet-name>reg</servlet-name>
<url-pattern>/reg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Catlog</servlet-name>
<url-pattern>/Catlog</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Profile</servlet-name>
<url-pattern>/Profile</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>order</servlet-name>
<url-pattern>/order</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

Output:

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

Result:

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

11.a Implement the "Hello World!" program using JSP Struts Framework

Aim:
To create and implement JSP Struts Framework to display HelloWorld! Message
Procedure:
1. Click on File Menu and choose New Project. A Dialog Box will be opened, choose
Java Web from catagoeries and Web application from Projects list.
2. Click on Next button, type HelloStruts in the Project name and click on Next.
3. Choose Apache Tomcat 6.0 or GlassFish V3 Domain from Server and press Next.
4. From Frameworks click on struts 1.3.8 checkbox, and finally click on Finish button.
5. It will open welcomeStruts.jsp page by default.

This is the Directory Structure of HelloStruts application


6. Right click on HelloStruts application and choose Java Package. Enter the package
name as com.naveen.form and click Finish.
7. Right click on the newly created Package New->Java Class. Enter the class name
as HelloWorldForm and click on Finish.
8. In the HelloWorldForm class add the following code.

package com.naveen.form;
import org.apache.struts.action.ActionForm;
public class HelloWorldForm extends ActionForm {
private static final long serialVersionUID = -473562596852452021L;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message; }}

9. In the same way create a new package com.arpit.action and create


a HelloWorldAction class, extending org.apache.struts.action.Action. And add
following code in action class.

package com.arpit.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.satish.form.HelloWorldForm;
public class HelloWorldAction extends Action {

St.Josephs College of Engineering ISO 9001:2008


CS6512 INTERNET PROGRAMMING LABORATORY 2015-2016

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
HelloWorldForm hwForm = (HelloWorldForm) form;
hwForm.setMessage(Hello World);
return mapping.findForward(success); } }
10. Add the following entries in the struts-config.xml file.
<?xml version=1.0 encoding=UTF-8 ?>
<!DOCTYPE struts-config PUBLIC
-//Apache Software Foundation//DTD Struts Configuration 1.3//EN
http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd>
<struts-config><form-beans><form-bean/></form-beans>
<global-exceptions></global-exceptions><global-forwards>
<forward path=/helloWorld.do/></global-forwards><action-mappings>
<action path=/helloWorld><forward path=/helloWorld.jsp />
</action></action-mappings>
11.Now configure the deployment descriptor. Add the following configuration information
in the web.xml file.
12. When we run the application the index.jsp page will be executed first. In the index.jsp
page we redirect the request to the helloWorld.do URI, which inturn invokes
the HelloWorldAction.
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<logic:redirect forward="helloWorld"/>
Add these lines of code in index.jsp page.
13. In the action class we return the ActionForward success which is mapped to
thehelloWorld.jsp page. In the helloWorld.jsp page we display the Hello World message.
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-">
<title>Hello World</title>
</head>
<body>
<bean:write name="HelloWorldForm" property="message"/>
</body>
</html>
Output:

Result:

St.Josephs College of Engineering ISO 9001:2008

You might also like