java

 pract 6 implicit object


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Implicit Object Example</title>

</head>

<body>

<h1>Implicit Object Example</h1>

<%-- Implicit Object: request --%>

<p>Request URL: <%= request.getRequestURL() %></p>

<%-- Implicit Object: response --%>

<p>Response Character Encoding: <%= response.getCharacterEncoding() %></p>

<p>Welcome, <%= session.getAttribute("username") %>!</p>

<%-- Implicit Object: application --%>

<% application.setAttribute("visitors", 1000);

%>

<p>Total Visitors: <%= application.getAttribute("visitors") %></p>

</body>

</html>




 pract 5

login.jsp

<html>

<head> <title> Login page </title>

</head>

<body>

<form action="validation.jsp">

<table border="0">

<tr><td> USER ID: </td><td>

<input type="text" name="uname" /> <br>

</td></tr><tr><td> PASSWORD: </td><td>

<input type="password" name="password" /> <br>

</td></tr><tr> <td align ="center">

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

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

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

</body></html>  validation.jsp

<html><body>

<%! String uid="student"; %>

<%! String pass="aitmca"; %>

<%! String id, password; %>

<% id=request.getParameter("uname"); %>

<% password=request.getParameter("password"); %>

<% if(uid.equals(id)&&pass.equals(password)){

%>

<jsp:forward page="welcome.jsp"/>

<%

}else{%>

<jsp:forward page="error.jsp" />

<%}%>

</body></html>

 

Welcome.jsp

<html>

<body bgcolor="pink"><center>

<%! String id; %>

<% id=request.getParameter("uname"); %>

<h1> welcome <%=id%> to the home page </h1></center>

</body></html> error.jsp

<html>

<head> <title>JSP Page</title> </head>

<body bgcolor="pink">

<h1>INVALID ENTRY</h1>

</body>

</html>

 




** Pract 8th**

 

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>demospring</groupId>

  <artifactId>demospring</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <name>Spring-Example</name>

  <dependencies>

        <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-core</artifactId>

    <version>6.1.1</version>

</dependency>

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>6.1.1</version>

</dependency>

 </dependencies>

</project>

 

Beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

 <!-- bean definitions here -->

    <bean id="HelloBean" class="demospring.HelloBean">

    <property name="name" value="world"></property>

  </bean>

</beans>

 

HelloBean.java

package demospring;

public class HelloBean {

       private String name;

      public String getName() {

              return name;

       }

        public void setName(String name) {

              this.name = name;

}

       public void sayHello() {

              System.out.println("Hello " +this.name);

       }

}

 

Main.java package demospring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {

               private static ApplicationContext context;  public static void main(String arg[])

               {

                     context = new ClassPathXmlApplicationContext("beans.xml");

                              HelloBean hellobean = (HelloBean) context.getBean("HelloBean");              hellobean.sayHello();

               }

}

 

Output :-

 



** Pract 10th And 11th **

 

Employee.java

package demo; public class Employee {

 

       private int id;

    private String name;     private double salary;

 

    // Constructor

    public Employee(int id, String name, double salary) {         this.id = id;         this.name = name;         this.salary = salary;

    }

 

       public int getId() {

              return id;

       }

 

       public void setId(int id) {

              this.id = id;

       }

 

       public String getName() {

              return name;

       }

 

       public void setName(String name) {       this.name = name;

       }

 

       public double getSalary() {

              return salary;

       }

 

       public void setSalary(double salary) {             this.salary = salary;

       }

}

 

 

EmployeeController.java

package demo;

public class EmployeeController {   private Employee model;      private EmployeeView view;

           // Constructor

           public EmployeeController(Employee model, EmployeeView view) {

               this.model = model;          this.view = view;

           }

 

           // Getters and setters      public int getId() {

               return model.getId();

           }

 

           public void setId(int id) {                model.setId(id);

           }

 

           public String getName() {

               return model.getName();

           }

 

           public void setName(String name) {

               model.setName(name);

           }

 

           public double getSalary() {          return model.getSalary();

           }

           public void setSalary(double salary) {

               model.setSalary(salary);

           }

 

           // Update view with employee data      public void updateView() {               view.printEmployeeDetails(model);

           }

        

 

 

}

 

 

EmployeeView.java

package demo;

 

public class EmployeeView {      public void printEmployeeDetails(Employee employee) {

        System.out.println("Employee Details:");

        System.out.println("ID: " + employee.getId());

        System.out.println("Name: " + employee.getName());

        System.out.println("Salary: $" + employee.getSalary());

    }

 

}

 

 

 

Main.java

package demo;

 

public class Main {

 

       public static void main(String[] args) {

              // Create model object

        Employee model = new Employee(101, "John Doe", 50000.00);

 

        // Create view object

        EmployeeView view = new EmployeeView();

 

        // Create controller object

        EmployeeController controller = new EmployeeController(model, view); 

        // Update view with employee data

        controller.updateView();

       }

 

}

 

Output :- 

 


 

Pract NO :_09

A.java

package com.java;   public class A {   public void m(){System.out.println("actual business logic");}  

}  

 

BeforeAdvisor.java

package com.java;   import java.lang.reflect.Method;   import org.springframework.aop.MethodBeforeAdvice;   public class BeforeAdvisor implements MethodBeforeAdvice{  

    @Override  

    public void before(Method method, Object[] args, Object target)throws Throwable {           System.out.println("additional concern before actual logic");  

    }  

}  

applicationContext.xml

<bean id="obj" class="com.java.A"></bean>  

<bean id="ba" class="com.java.BeforeAdvisor"></bean>  

<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">  

<property name="target" ref="obj"></property>  

<property name="interceptorNames">  

<list>  

<value>ba</value>  

</list>  

</property>  

</bean>  

Test.java

package com.java;   import org.springframework.beans.factory.BeanFactory;   import org.springframework.beans.factory.xml.XmlBeanFactory;   import org.springframework.core.io.ClassPathResource;   import org.springframework.core.io.Resource;   public class Test {  

public static void main(String[] args) {  

    Resource r=new ClassPathResource("applicationContext.xml");  

    BeanFactory factory=new XmlBeanFactory(r);  

      

    A a=factory.getBean("proxy",A.class);       a.m();  

}  

}  

 

OUTPUT

additional concern before actual logic   actual business logic  

 

 

Q. 1)write a java servlet program using cookies to remember user preferences.   

   

Index_2.html     

<body>    <h1>Login Session</h1>    <a href="Login_2.html">Login</a>    <a href="LogoutServlet_2">Logout</a>    <a href="MyServlet_2">Profile</a>    </body>     

     

Login_2.html     

<body>    <form align="center" action="LoginServlet_2" method="post">   <b>Name :-< /b>    <input type="text" name="name"><br><br>  <b>Password :-< /b>    <input type="text" name="pass"><br><br>    <input type="submit" value="login">    </form>    </body>     

Link_2.html     

<body   

<a href="Login_2.html">Login</a>     

<a href="LogoutServlet_2">Logout</a>     <a href="MyServlet_2">Profile</a>   </body>   

 

 

 

 

 

 

 

 

 

 

LoginServlet_2.java     

package all_pract;     import java.io.PrintWriter;    public class LoginServlet_2 extends HttpServlet {          protected void doPost(HttpServletRequest request, HttpServletResponse  response) throws ServletException, IOException {    

        response.setContentType("text/html");        

 PrintWriter out=response.getWriter();      

   request.getRequestDispatcher("Link_2.html"). include(re quest, response);      

   

      String

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

password=request.getParameter("password"

);           

if(password.equals("123")){          out.print("Welcome, "+name);       

      HttpSession session=request.getSession();        session.setAttribute("name",name);      

      }            else{         out.print("Sorry,  username  or  password  error!");      request.getRequestDispatcher("Login_2.html")

.include( r equest, response);     

    

      }        

  out.close();     

}     

}     

 

 

 

 

 

LogoutServlet_2.java     

package all_pract;    importjava.io.PrintWrite;    public class LogoutServlet_2 extends HttpServlet {     

    

}     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {   

   

       response.setContentType("text/html");         

PrintWriter out=response.getWriter();      

     request.getRequestDispatcher("Link_2.html"). include(re quest, response);    

          

 HttpSession session=request.getSession();       session.invalidate();      out.print("You are successfully logged out!");             out.close();     

   }         

   }     

     

  

   

MyServlet_2.java     

package all_pract;    import java.io.PrintWriter;      public class MyServlet_2 extends HttpServlet {      

 }     

   protected void doGet(HttpServletRequest request,   

HttpServletResponse  response) throws   

ServletException, IOException {   

         response.setContentType("text/html");        

 PrintWriter out=response.getWriter();      

   request.getRequestDispatcher("Link_2.html"). include(re quest, response);            

HttpSession

session=request.getSession(false);     

       if(session!=null){      

    String name=(String)session.getAttribute("name");        out.print("Hello, "+name+" Welcome to Profile");           }             else{           

out.print("Please login first");    request.getRequestDispatcher("Login_2.html")

.include(r equest, response);    

      }           out.close();     

   }     

   }

Q.2) Arithmetic op Program Servlet? 

  

Index.html   

<body>   

<form action="pract1" method="Post" align="center">   

<label>First number</label>   

<input type="text" name="num1"/><br><br><br>   

<label>Second number</label>   

<input type="text" name="num2"/><br/><br/>   

<button

type="submit"name="Submit">Submit</button><br/>

</form>   

 

Servletpage  package demo;  import java.io.*;

 

 public class pract1 extends HttpServlet {

 protected void doPost(HttpServletRequest request,

HttpServletResponse response) throws ServletException,

IOException {

 

 int num1=Integer.parseInt(request.getParameter("num1"));  int num2=Integer.parseInt(request.getParameter("num2"));

 

int add=num1+num2; int sub=num1-num2;     int mul=num1*num2;  int div=num1/num2;  int mod=num1%num2;  PrintWriter output =response.getWriter();

 

 output.println("Add No;-"+add);  output.println("Sub No;-"+sub);  output.println("Mul No;-"+mul);  output.println("div No;-"+div);  output.println("Mod No;-"+mod); 

} } 

 



 

Even Odd Program



Roll_No: 40

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"     pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body> <%

out.print("Even Number<br>\n");

for(int i=1;i<=20;i++){  if(i%2==0){    out.println(i);

     }

}

out.print("<br>Odd Number<br>");

for(int i=1;i<=20;i++){  if(i%2!=0){

          out.println(i);

     }

}

%>

</body>

</html>

 

 

O/P

Even Number

2 4 6 8 10 12 14 16 18 20

Odd Number

1 3 5 7 9 11 13 15 17 19

 



Regist Form

Form.jsp  

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"      pageEncoding="ISO-8859-1"%> 

<!DOCTYPE html> 

<html> 

<head> 

<meta charset="ISO-8859-1">  

<title>   

Registration Page   

</title>  

<style> .a

border:1px solid red; padding:20px

} h1{ color:red; }  </style> 

</head>   

<body bgcolor="Lightskyblue" >   

<br>   

<br>   

<div> 

<form action="Ragistration.jsp" border="1" class="a" align="center">  

  <h1>Registration_Form</h1> 

<label> Firstname </label>          

<input type="text" name="uname" size="15"/> <br> <br>   

<label> Middlename: </label>      

<input type="text" name="middlename" size="15"/> <br> <br>   

<label> Lastname: </label>          

<input type="text" name="lastname" size="15"/> <br> <br>   

   

<label>    Course :   

</label>    

<select>   

<option value="Course">Course</option>   

<option value="BCA">BCA</option>    <option value="BBA">BBA</option>   

<option value="B.Tech">B.Tech</option>   

<option value="MBA">MBA</option>    <option value="MCA">MCA</option>   

<option value="M.Tech">M.Tech</option>   

</select>   

   

<br>   

<br>   

<label>    Gender :   

</label><br>   

<input type="radio" name="male"/> Male <br>   

<input type="radio" name="female"/> Female <br>   

<input type="radio" name="other"/> Other   

<br>   

<br>   

   

<label>     Phone :   

</label>   

<input type="text" name="country code"  value="+91" size="2"/>    

<input type="text" name="phone" size="10"/> <br> <br>   Address   

<br>   

<textarea cols="40" rows="5" value="address">   

</textarea>   <br> <br>   

Email:   

<input type="email" id="email" name="email"/> <br>      <br> <br>   Password:   

<input type="Password" id="pass" name="pass"> <br>    

<br> <br>   

Re-type password:   

<input type="Password" id="repass" name="repass"> <br> 

<br>   

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

</form>  </div> 

 

</body> 

</html> 

  

  

RegistrationForm  

<%@ page language="java"

contentType="text/html; charset=ISO-8859-1"     pageEncoding="ISO-8859-1"%>  <!DOCTYPE html> 

<html> 

<head> 

<meta charset="ISO-8859-1"

<title>Insert title here</title> 

</head> 

<body> 

<%  

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

 

out.print(name); 

%> 

</body> 

</html> 


 

Comments