Вы находитесь на странице: 1из 16

LAB 2

ACTIVITY 1

CODING OUTPUT
<%--
Document : Activity1
Created on : Aug 22, 2019, 8:20:46 AM
Author : P300
--%>

<HTML>
<head>
<!-- this is a comment tag -->
<title>Lab8</title>
</head>
<body>
<font face=verdana color=darkblue>
<H1>Activity</H1>
<br><br>
<!-- Declaration tag -->
<%!
public String writeThis(int x)
{
String myText="";
for (int i = 1; i <x; i++)
myText = myText+"<font size="+i+"
color-darked face=verdana>Fist JSP
script</font><br>";
return myText;
}
%>
This is a loop example:
<br>
<!-- Expression tag -->
<%= writeThis(8) %>
<br><br>
<%
String username = "JSP";
out.println ("Hi i am "+username );
%>
</font>
</body>
</HTML>
ACTIVITY 2

CODING OUTPUT
<%--
Document : Activity2
Created on : Aug 22, 2019, 8:35:48 AM
Author : P300
--%>

<HTML>
<HEAD>
<TITLE>Activity 2</TITLE>
</HEAD>
<BODY>
<font face=verdana color=darkblue>
<h1>Using JavaBeans with JSP</h1>
<jsp:useBean id="stringBean"
class="coreservlets.StringBean" />
<OL>
<LI>Initial value (getProperty):
<I><jsp:getProperty
name="stringBean"
property="message" /></I>
<LI>Initial value (JSP expression):
<I><%=
stringBean.getMessage()%></I>
<LI><jsp:setProperty name="stringBean"
property="message"
value="New Message: I love JAVA" />
Value after setting property with
setProperty:
<I><jsp:getProperty
name="stringBean" property="message" /></I>
<LI><% stringBean.setMessage("My
favorite language: JAVA"); %>
Value after setting property with
scriptlet:
<I><%= stringBean.getMessage() %></I>
</OL>
</font>
</BODY>
</HTML>

/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/
package coreservlets;

/**
*
* @author P300
*/
public class StringBean {
private String message = "This is how you use
String Bean in JSP";
public String getMessage() {
return(message);
}
public void setMessage(String message){
this.message= message;
}
}

ACTIVITY 3

CODING OUTPUT
<!--
To change this template, choose Tools |
Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<head>
<title>Activity 3</title>
</head>
<body>
<font face=verdana color=darkblue>
<h1> Passing Value </h1>
<form method="POST"
action="Activity3.jsp">

Name:<input type="text" name="name"


size="20"><br>
I/C Number:<input type="text" name="ic"
size="20"><br>
Gender:
<input type="radio" name="sex"
value="male">Male
<input type="radio" name="sex"
value="female">Female<br>
Favorite Programming Language:
<select name="lang">
<option value="C/C++">C/C++</option>
<option value="JAVA">JAVA</option>
<option value="PHP">PHP</option>
<option value="ASP">ASP</option>
</select><br>
<p><input type="submit" value="Submit"
name="B1"></p>
</form>
</font>
</body>
</html>
<%--
Document : Activity3
Created on : Aug 22, 2019, 9:20:30 AM
Author : P300
--%>

<%@page contentType="text/html" %>


<html>
<title> Activity 3</title>
<body>
<font face=verdana color=darkblue>
<h1> Received Value </h1>
You have enter:<br><br>
Name:
<%=request.getParameter("name")%><br>
I/C Number:
<%=request.getParameter("ic")%><br>
Gender:
<%=request.getParameter("sex")%><br>
Favorite Programming
Language:<%=request.getParameter("lang")%><b
r>
</font>
</body>
</html>

ACTIVITY 4

CODING OUTPUT
<%--
Document : First
Created on : Aug 22, 2019, 9:35:47 AM
Author : P300
--%>

<html>
<head>
<title>Demo of JSP Param Action
Tag</title>
</head>
<body>
<h3>JSP page: Demo Param along with
forward</h3>
<jsp:forward page="Second.jsp">
<jsp:param name ="date" value="20-05-
2012"/>
<jsp:param name ="time"
value="10:15AM"/>
<jsp:param name ="data" value="ABC"/>
</jsp:forward>
</body>
</html>

<%--
Document : Second
Created on : Aug 22, 2019, 9:40:14 AM
Author : P300
--%>

<htm>
<head>
<title>Demo of JSP Param Action
Tag</title>
</head>
<body>
<h3>JSP page: Demo Param along with
forward</h3>
Date:<%= request.getParameter("date")%>
Time:<%= request.getParameter("time")%>
My Data:<%=
request.getParameter("data")%>
</body>
</htm>

ACTIVITY 5

CODING OUTPUT
<%--
Document : main
Created on : Aug 22, 2019, 9:52:19 AM
Author : P300
--%>

<html>
<head>
<title>Include Directive</title>

<style>
div.container {
width: 100%;
border: 1px solid gray;
}

header,footer{
padding: 1em;
color: white;
background-color: black;
clear: left;
text-align: center;
}
nav {
float: left;
max-width: 160px;
margin: 0;
padding: 0;
}

nav ul {
list-style-type: none;
padding: 0;
}

nav ul a{
text-decoration: none;
}

article {
margin-left: 170px;
border-left: 1px solid gray;
padding: lem;
overflow: hidden;
}
</style>
</head>
<body>
<div class="container">

<header>
<%@ include file = "header.jsp"%>
</header>

<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Products</a></li>
</ul>
</nav>

<article>
<h1>Seberang Perai Polytechnic</h1>
<p>Seberang Perai Polytechnic was
established on 1st September 1998 at the Jalan
Permatang Pauh, Pulau Pinang. This campus
was built in an area
of 100 acres. Psp is located in the
rapidly growing industrial area and it takes
about 30 minutes to reach here from
Georgetown, Pulau Pinang.</p>
<p>This polytechnic is the 10th
polytechnic being set up in Malaysia in line with
the government policy to produce semi-
professional workers
in order to achieve the Malaysia 2020
Vision. PSP has been officially launched on 27
November 1999 by former 4th Malaysia Prime
Minister, Tun Dr.
Mahathir Mohamad.</p>
<p>PSP offers full time and part-time
programme for Certificate and Diploma level in
the field of Electrical Engineering, Mechanical
Engineering,
Commerce and Information
Technology</p>
</article>

<footer><%@ include file =


"footer.jsp"%></footer>
</div>
</body>
</html>

<%--
Document : header
Created on : Aug 22, 2019, 10:11:58 AM
Author : P300
--%>

<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
<style>
header, footer{
padding: 1em;
color: white;
background-color: black;
clear: left;
text-align: center;
}

</style>
</head>
<body>
<header>
<h1>Seberang Perai Polytechnic</h1>
</header>
</body>
</html>

<%--
Document : footer
Created on : Aug 22, 2019, 10:15:11 AM
Author : P300
--%>

<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
<style>

header, footer{
padding: 1em;
color: white;
background-color: black;
clear: left;
text-align: center;
}
</style>
</head>
<body>
<footer>Copyright &copy; Seberang Perai
Polytechnic</footer>
</body>
</html>
ACTIVITY 6

CODING OUTPUT
<%--
Document : Activity4
Created on : Aug 22, 2019, 10:26:46 AM
Author : P300
--%>

<%@ page language="java"


import="java.sql.*" %>
<html>
<head>
<title>Activity 4</title>
<script language="javascript">
function validate(objForm) {

if(objForm.bookname.value.length==0) {
alert("Please enter Book Name!");
objForm.bookname.focus();
return false;
}
if(objForm.author.value.length==0) {
alert("Please enter Author
name!");
objForm.author.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<font face="Trebuchet MS"
color="darkblue">
<h1> Connecting JDBC to Database:
Inserting Data</h1>
<form action="Activity4.jsp"
method="post" name="entry"
onsubmit="return validate(this)">
<input type="hidden" value="list"
name="action">
<table border="1" cellpading="0"
cellspacing="0">
<tr>
<td>
<table>
<tr><td colspan="2"
align="center"><h2>Book Entry
Form</h2></td></tr>
<tr><td
colspan="2">&nbsp;</td></tr>
<tr>
<td>Book Name:</td>
<td><input
name="bookname" type="text"
size="50"></td>
</tr>
<tr>
<td>Author:</td><td><input
name="author" type="text" size="50"></td>
</tr>
<tr>
<td colspan="2"
align="center">
<input type="submit"
value="Submit"></td>

</tr>

</table>
</td>
</tr>
</form>

<%
try{

Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://l
ocalhost/books","root","");
Statement stmt=
con.createStatement();

if(request.getParameter("action") !=
null) {
String
bookname=request.getParameter("bookname
");
String
author=request.getParameter("author");

stmt.executeUpdate("insert into
books_details(book_name,author)values('"+b
ookname+"','"+author+"')");
con.close();
}
}
catch(Exception e) {
System.out.println(e.getMessage());
}
%>
</font>
</body>
</html>
<%@ page language="java" import
="java.sql.*"%>
<html>
<head>
<title>Activity 5</title>
</head>
<body>
<font face=verdana color=darkblue>
<h1> Connecting JDBC to Database:
Displaying Data</h1>
<%
try {
// Step 1.Load the JDBC driver

Class.forName("com.mysql.jdbc.Driver");

//Step 2.Create a Connection object


Connection con =

DriverManager.getConnection("jdbc:mysql://l
ocalhost/books","root","");
System.out.println("got connection");

//Step 3.Create a Statement object


and call its executeUpdate
//method to insert a record
Statement s = con.createStatement();

//Step 4.Use the same Statement


object to obtain a ResultSet object
String sql = "SELECT * FROM
books_details";
ResultSet rs = s.executeQuery(sql);
%>
<h2>Books List</h2>
<table border="1" cellspacing="0"
cellpadding="0">
<tr>
<td><b>No</b></td>
<td><b>Book Name</b></td>
<td><b>Author</b></td>
</tr>
<%
int no=1;
while(rs.next()) {
%>
<tr>
<td><%=no%></td>
<td><%=rs.getString("book_name")%></td>
<td>
<%=rs.getString("author")%> </td>
</tr>
<%
no++;
}
%>
</table>
<%
rs.close();
s.close();
con.close();

}
catch (Exception e) {
// unexpected exception, print error
message to the console
System.out.println(e.toString());
}
%>
</font>
</body>
</html>

Activity 6

CODING OUTPUT
<%--
Document : Activity6
Created on : Aug 29, 2019, 8:31:44 AM
Author : P300
--%>

<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head><title>Activity 6</title></head>
<body>
<font face="Trebuchet MS"
color="darkblue">

<%
if(request.getAttribute("error")!=null){
out.print("Not valid user! Try again<hr>");
}
%>
<form action="MServlet">
<h4>Login</h4>
Name:<input type="text"
name"name"/><br>
password:<input type="password"
name="pass"/><br>

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


<input type="reset" value="Clear"/>

</form>
</font>
</body>
</html>
<%--
Document : welcome
Created on : Aug 29, 2019, 8:32:30 AM
Author : P300
--%>

<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Activity 6</title>
</head>
<body>
<font face="Trebuchet MS"
color="darkblue">
<h1>Welcome<%
out.print(request.getParameter("name"));
%></h1>
</font>
</body>
</html>
/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author P300
*/
@WebServlet(urlPatterns = {"/MServlet"})
public class MServlet extends HttpServlet {

/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific
error occurs
* @throws IOException if an I/O error occurs
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

String n=request.getParameter("name");
String p=request.getParameter("pass");

if(n==null){
RequestDispatcher
rd=request.getRequestDispatcher("Activity6.jsp");
rd.forward(request, response);
}
else
if(n.equals("HidayahM")&&(p.equals("pass"))){
RequestDispatcher
rd=request.getRequestDispatcher("welcome.jsp")
;
rd.forward(request, response);
}
else{
request.setAttribute("error","true");
RequestDispatcher
rd=request.getRequestDispatcher("Activity6.jsp");
rd.forward(request, response);
}
out.close();
}

}
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>Home</title>
</head>
<body>
<%
Connection con= null;
PreparedStatement ps = null;
ResultSet rs = null;

String driverName =
"com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/user";
String user = "root";
String password = "";
String sql = "select usertype from
user_details";

try{
Class.forName(driverName);
con= DriverManager.getConnection(url,
user, password);
ps = con.preparedStatement (sql);
rs = ps.executeQuery();
%>

<form method="post"
action="Login.jsp">
<center><h2 style="color:green">JSP
Login Example</h2></center>
<table border="1" align="center">
<tr>
<td>Enter Your Name :</td>
<td><input type="text"
name="name"/></td>
</tr>
<tr>
<td>Enter Your Password :</td>
<td><input type="password"
name="password"/></td>
</tr>
<tr>
<td>Select UserType</td>
<td><Select name="usertype">
<option
value="select">select</option>
<%
while(rs.next())
{
String usertype =
rs.getString("usertype");
%>
<option
value=<%=usertype%>><%=usertype%></option>
<%
}
}
catch(SQLException sqe)
{
out.println("home"+sqe);
}
%>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit"
value="submit"/></td>
</table>
</form>
</body>
</html>

Вам также может понравиться