AdvancedJava Lab Journal Report
AdvancedJava Lab Journal Report
AdvancedJava Lab Journal Report
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Submit Name</title>
</head>
<body>
<form action="index.jsp" method="GET">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<button type="submit">Submit</button>
</form>
</body>
</html>
Index.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Hello<%=request.getparameter(“name”)%></h1>
</body>
</html>
Output
responseExample.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
<%
%>
</body>
</html>
Output
exceptionExample.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Division Result</title>
</head>
<body>
<%
try {
%>
<h2>Result of Division</h2>
<%
} catch (Exception e) {
response.sendRedirect("error.jsp?message=" +
java.net.URLEncoder.encode(e.getMessage(), "UTF-8"));
%>
</body>
</html>
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Calculator</title>
</head>
<body>
<h2>Calculator</h2>
</form>
</body>
</html>
Output
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/redirect")
response.sendRedirect("https://www.google.com");
doGet(request, response);
Output
5.Write a java program to pass the parameters to next page
Page1.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Page 3</title>
</head>
<body>
<%
%>
</body>
</html>
Page2.jsp
<html>
<head>
<meta charset="UTF-8">
<title>Page 2</title>
</head>
<body>
<%
session.setAttribute("name", name);
%>
</body>
</html>
Page3.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Page 3</title>
</head>
<body>
<%
%>
</body>
</html>
Output
6.write a java program for javabean
Index.jsp
<%--
Document : index
Author : otaxa
--%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
int m=obj.cube(5);
out.println("Cube is"+m);
%>
</body>
</html>
Calculator.java
package MCA;
return n*n*n;
Output
7.write a java program Session Management
Session.jsp
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%
if(session.isNew())
title="welcome";
session.setAttribute(userIDKey,userID);
session.setAttribute(visitCountKey,visitCount);
visitCount=(Integer)session.getAttribute(visitCountKey);
visitCount+=1;
userID=(String)session.getAttribute(userIDKey);
session.setAttribute(visitCountKey,visitCount);
%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<center>
<h1>Session tracking</h1>
</center>
<tr bgcolor="#949494">
<th>session info</th>
<th>value</th>
</tr>
<tr>
<td>id</td>
<td><%out.print(session.getId());%></td>
</tr>
<tr>
<td>Creation Time</td>
<td><%out.print(createTime);%></td>
</tr>
<tr>
<td><%out.print(lastAccessTime);%></td>
</tr>
<tr>
<td>User ID</td>
<td><%out.print(userID);%></td>
</tr>
<tr>
<td>Number of visit</td>
<td><%out.print(visitCount);%></td>
</tr>
</table>
</body>
</html>
Output
HelloInterface.java
import java.rmi.Remote;
import java.rmi.RemoteException;
}
HelloImpl.java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
super();
@Override
Server.java
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
try {
Naming.rebind("HelloServer", obj);
System.out.println("Server is running...");
} catch (Exception e) {
e.printStackTrace();
}
Client.java
import java.rmi.Naming;
try {
} catch (Exception e) {
e.printStackTrace();
Output
9.write a java program for Hibernate
Hibernate.cfg.xml
<!--
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://127.0.0.1:3306/emp
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
</property>
<mapping resource="hibernatedemo1/Employee2.hbm.xml"/>
<mapping resource="hibernatedemo1/Employee2.hbm.xml"/>
<mapping resource="hibernatedemo1/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Employee.hbm.xml
<!--
-->
<hibernate-mapping>
</meta>
<generator class="native"/>
</id>
</hibernate-mapping>
Employee.java
/*
*/
package hibernatedemo1;
/**
* @author otaxa
*/
public Employee1() {}
this.firstName = fname;
this.lastName = lname;
this.salary = salary;
return id;
this.id = id;
}
return firstName;
this.firstName = first_name;
return lastName;
this.lastName = last_name;
return salary;
this.salary = salary;
HibernateDemo1.java
/*
*/
package hibernatedemo1;
import java.util.List;
import java.util.Date;
import java.util.Iterator;
import javax.imageio.spi.ServiceRegistry;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* @author otaxa
*/
/**
*/
try {
//try {
//}
ME.listEmployees();
ME.updateEmployee(empID1, 5000);
ME.deleteEmployee(empID2);
ME.listEmployees();
Transaction tx = null;
try {
tx = userSession.beginTransaction();
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
userSession.close();
return employeeID;
Transaction tx = null;
try {
tx = session.beginTransaction();
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
/* Method to UPDATE salary for an employee */
Transaction tx = null;
try {
tx = session.beginTransaction();
employee.setSalary( salary );
session.update(employee);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.delete(employee);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
Output
Fileupload.jsp
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<h1>Upload a File</h1>
</form>
</body>
</html>
Uploadsuccess.jsp
<!DOCTYPE html>
<html>
<head>
<title>Upload Success</title>
</head>
<body>
</body>
</html>
FileUploadAction.java
package java.io;
public void write(byte b[], int off, int len) throws IOException {
if (b == null) {
} else if (len == 0) {
return;
write(b[off + i]);
FileUploadForm.java
package com.example.forms;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
/**
* @author MCA
*/
return file;
this.file = file;
Structs-config.xml
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
</form-beans>
<action-mappings>
input="/fileUploadForm.jsp">
</action-mappings>
</struts-config>
output