Advance Web Technology Final Journal

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

INDEX

Teacher's
Sr.No. Practical Aim Date
Signature
1 Write a program in C# to store Array.

2 Write a program in C# to create class.

3 Write a program in C# to demonstrate inheritance.

4 Write a program to implement servlet .


Write a Servlet program to calculate factorial of
5
number.
6 Write a Servlet program to demonstrate cookies.

7 Write a program in JSP to print fibonacci series.


Write a program in JSP to demonstrate attributes of
8
page directive tag.
Write a XML program to search the configuration in
9
web.xml.

Create a registration servlet in Java using JDBC.


Accept the details such as Username, Password,
10
Email, and Country from the user using HTML Form
and store the registration details in the database.
PRACTICAL No. 1
AIM: Write a program in C# to store Array.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace store_value_in_array
{
class Program
{
static void Main(string[] args)
{
int i;
int[] arr = new int[5]; // 5 size array

// Accepting value from user


for (i = 0; i < 5; i++)
{
Console.Write("\nEnter your number:\t");
//Storing value in an array
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("\n\n");
//Printing the value on console
for (i = 0; i < 5; i++)
{
Console.WriteLine("you entered {0}", arr[i]);
}
Console.ReadLine();
}
}
}
Output:
PRACTICAL No. 2
AIM: Write a program in C# to create class.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Creating_Class
{
class accept //Creating 1st. class
{
public string name;
public void acceptdetails()
{
Console.Write("Enter your name:\t");
name = Console.ReadLine();
}
}

class print // Creating 2nd class


{
public void printdetails()
{
//Creating object of 1st. class
accept a = new accept();
//executing method of 1st class.
a.acceptdetails();
//Printing value of name variable
Console.WriteLine("Your name is " +a.name);
}
}
class Program //Creating 3rd class
{
static void Main(string[] args)
{
print p = new print();
p.printdetails();
Console.ReadLine();
}
}
}
Output:
PRACTICAL No. 3
AIM : Write a program in C# to demonstrate inheritance.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Basic_Example
{
class Program
{
static void Main(string[] args)
{
Scooter sc = new Scooter();
sc.ScooterType();

Car c = new Car();


c.CarType();

Console.ReadKey();
}
}
//Creating Base Class
class Tyre
{
protected void TyreType()
{
Console.WriteLine("This is Tubeless Tyre");
}
}
//Creating Child Class
class Scooter : Tyre
{
public void ScooterType()
{
Console.WriteLine("Scooter Color is Red");
TyreType();
}
}
//Creating Child Class
class Car : Tyre
{
public void CarType()
{
Console.WriteLine("Car Type : Ferrari");
TyreType();
}
}
}
Output:
PRACTICAL No. 04

AIM : Write a program to implement servlet .

Program:

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>

<form action="demo" >


Enter Your Name :- <input type="text" name="n1" ><br>
<input type="submit" value=" submit " >
</form>
</body>
</html>

demo.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class demo extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {

String s=request.getParameter("n1");
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
"Servlet Hello "+s );
out.println(
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title></title>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");

}
}
}

Output :-
Servlet Hello Students
PRACTICAL No. 05

AIM : Write a Servlet program to calculate factorial of number.

Program:

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>
<form method="get" action="FactorialServlet">
<table>
<tr>
<td>Enter a value to find its factorial</td>
<td><input type="text" name="text1"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Enter"/></td>
</tr>
</table>
</form>

</body>
</html>

FactorialServlet.java:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class demo extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {

int fact=1;
int n;
n = Integer.parseInt(request.getParameter("n1"));

response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {

for(int i=1;i<=n;i++)
{
fact=fact*i;
}

out.println("Factorial of given number"+n+" is :"+fact+" ");


out.println("<!DOCTYPE html>");

out.println("<html>");
out.println("<head>");
out.println("<title></title>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");

}
}
}

Output :-
PRACTICAL No. 06

AIM : Write a Servlet program to demonstrate cookies.

Program:

Index.html
<!DOCTYPE html>
<html>
<head>
<title>Setting cookies</title>
<meta charset=”UTF-8”>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>
</head>
<body>
<form method="get" action="HelloForm">
<table>
<tr>
<td>First Name :<input type="text" name="FirstName"/></td>
</tr>
<tr>
<td>Last Name :<input type="text" name="LastName"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
</form> </body>
<html>

HelloForm :

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class demo extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
Cookie FirstName =new
Cookie("FirstName",request.getParameter("FirstName"));
Cookie LastName = new
Cookie("LastName",request.getParameter("LastName"));

FirstName.setMaxAge(60*60*24);
LastName.setMaxAge(60*60*24);

response.addCookie(FirstName);
response.addCookie(LastName);
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {

String title="Setting Cookies Example";


out.println("First Name:"+request.getParameter("FirstName")+"Last
Name:"+request.getParameter("LastName")+"");
out.println("<!DOCTYPE html>");

out.println("<html>");
out.println("<head>");
out.println("<title></title>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");

}
}
}

Output :-
PRACTICAL No. 7

AIM : Write a program in JSP to print fibonacci series.


Program:
Index.jsp :

<%@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>
</head>
<body>
<h1>Enter the Limit of Fibonacci series</h1>
<form action="fibo.jsp">
Enter the Number :<input type="text" name="n1"/><br/>
<input type="submit" value="Enter"/>
</form>
</body>
</html>

Fibo.jsp:

<%@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>
</head>
<body>
<h1>The value of Fibonacci Series</h1>
<h1>
<%
String s =request.getParameter("n1");
int n=Integer.parseInt(s);
int i=1,f1=0,f2=1,f3;
while(i<=n)
{
out.println(f1);
f3 = f1 + f2;
f1 = f2;
f2 = f3;
i = i + 1;
}
%>
</h1>
</body>
</html>
Output:
PRACTICAL No. 8
AIM : Write a program in JSP to demonstrate attributes of page directive tag.
Program:
Index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Page attributes</title>
</head>
<body>
<form action="Directive.jsp" method="get">
<h1>Enter the value of n1 and n2 :</h1>
Number1 :<input type="number" name="n1"/><br/>
Number2 :<input type="number" name="n2"/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>

Directive.jsp :

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<%@page import="java.util.*" %>
<%@page info="composed by CITECH" %>
<%@page language="java" %>
<%@page buffer="16kb" %>
<%@page autoFlush="true" %>
<%@page isThreadSafe="true" %>
<%@page errorPage="error.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Page attributes</title>
</head>
<body bgcolor="green">
<h1><u>Usage of import attributes</u></h1>
<h2>Today's date is:<%= new Date()%></h2>
<h2> To see the error page enter n2 value as zero and click submit</h2>
<%
int n1=Integer.parseInt(request.getParameter("n1"));
int n2=Integer.parseInt(request.getParameter("n2"));
%>
<h2>Value of n1/n2==> <%= n1/n2%></h2>
</body>
</html>

Error.jsp :

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<%@page errorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Page attributes</title>
</head>
<body>
<h2>Value of n2 variable of Zero(n/0 is infinity)</h2>
<h3>SORRY an exception occured !!</h3><br/>
</body>
</html>
Output:
PRACTICAL No. 9
AIM : Write a XML program to search the configuration in web.xml.
Program :
ErrorHandler.java :
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

// Extend HttpServlet class


public class ErrorHandler extends HttpServlet {

// Method to handle GET method request.


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

// Analyze the servlet exception


Throwable throwable = (Throwable)
request.getAttribute("javax.servlet.error.exception");
Integer statusCode = (Integer)
request.getAttribute("javax.servlet.error.status_code");
String servletName = (String)
request.getAttribute("javax.servlet.error.servlet_name");

if (servletName == null) {
servletName = "Unknown";
}
String requestUri = (String)
request.getAttribute("javax.servlet.error.request_uri");

if (requestUri == null) {
requestUri = "Unknown";
}

// Set response content type


response.setContentType("text/html");

PrintWriter out = response.getWriter();


String title = "Error/Exception Information";
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");

if (throwable == null && statusCode == null) {


out.println("<h2>Error information is missing</h2>");
out.println("Please return to the <a href=\"" +
response.encodeURL("http://localhost:8080/") +
"\">Home Page</a>.");
} else if (statusCode != null) {
out.println("The status code : " + statusCode);
} else {
out.println("<h2>Error information</h2>");
out.println("Servlet Name : " + servletName + "</br></br>");
out.println("Exception Type : " + throwable.getClass( ).getName( ) + "</br></br>");
out.println("The request URI: " + requestUri + "<br><br>");
out.println("The exception message: " + throwable.getMessage( ));
}
out.println("</body>");
out.println("</html>");
}

// Method to handle POST method request.


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

doGet(request, response);
}
}

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>ErrorHandler</servlet-name>
<servlet-class>ErrorHandler</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ErrorHandler</servlet-name>
<url-pattern>/ErrorHandler</url-pattern>
</servlet-mapping>
<!-- error-code related error pages -->
<error-page>
<error-code>404</error-code>
<location>/ErrorHandler</location>
</error-page>

<error-page>
<error-code>403</error-code>
<location>/ErrorHandler</location>
</error-page>

<!-- exception-type related error pages -->


<error-page>
<exception-type>
javax.servlet.ServletException
</exception-type >
<location>/ErrorHandler</location>
</error-page>

<error-page>
<exception-type>java.io.IOException</exception-type >
<location>/ErrorHandler</location>
</error-page>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Output:
PRACTICAL No. 10

AIM : Create a registration servlet in Java using JDBC. Accept the details such as Username,
Password, Email, and Country from the user using HTML Form and store the registration
details in the database.
Program:

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>
<center>
<form action="NewServlet" method="post">
USER NAME :-<input type="text" name="u" size="20">
<br>
PASSWORD :-<input type="text" name="p" size="20">
<br>
FIRST NAME :-<input type="text" name="f" size="20">
<br>
MIDDLE NAME :-<input type="text" name="m" size="20">
<br>
LAST NAME :-<input type="text" name="l" size="20">
<br>
EMAIL ID :-<input type="text" name="e" size="20">
<br>
<input type="submit" value="SUBMIT">
</form>
</center>
</body>
</html>

NewServlet.java
package newser;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author DELL
*/
public class NewServlet 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
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

PrintWriter pw = response.getWriter();
String connectionURL = "jdbc:mysql://localhost:3306/demo";// demo is the database
Connection connection;

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

String Username = request.getParameter("u");


String Password= request.getParameter("p");
String firstname = request.getParameter("f");
String middlename= request.getParameter("m");
String lastname = request.getParameter("l");
String email= request.getParameter("e");

Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "root123");
PreparedStatement pst = connection.prepareStatement("insert into
student(username,password,firstname,middlename,lastname,email) values(?,?,?,?,?,?)");
pst.setString(1,Username);
pst.setString(2,Password);

int i = pst.executeUpdate();
if(i!=0){
pw.println("<br>Record has been inserted");

}
else{
pw.println("failed to insert the data");
}
}
catch (Exception e){
pw.println(e);
}

/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");

Output :-

You might also like