0% found this document useful (0 votes)
35 views

Adv Java

This document provides examples of creating a basic Spring application that reads configuration values, injecting primitive values using setter injection, and demonstrating JSP scripting elements, directives, implicit objects, action tags, and validating a username and password. It includes code samples for a Spring Maven project, reading configuration values, injecting values into a bean, and JSP pages for including content, setting variables, and form validation.

Uploaded by

0211cse089
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Adv Java

This document provides examples of creating a basic Spring application that reads configuration values, injecting primitive values using setter injection, and demonstrating JSP scripting elements, directives, implicit objects, action tags, and validating a username and password. It includes code samples for a Spring Maven project, reading configuration values, injecting values into a bean, and JSP pages for including content, setting variables, and form validation.

Uploaded by

0211cse089
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Create the First Spring framework core Application to add required dependency in
pom.xml file using eclipse/other framework and print the value from configuration
file
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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-core-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- Spring Core dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
</dependency>
</dependencies>
</project>

App.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {


public static void main(String[] args) {
// Load the Spring configuration file
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");

// Get the value from the configuration file


String message = context.getBean("message", String.class);

// Print the value


System.out.println("Message from configuration file: " + message);
}
}

applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- Define a property value -->


<bean id="message" class="java.lang.String">
<constructor-arg value="Hello, Spring!"/>
</bean>

</beans>

2. Write the program in spring to inject primitive and string- based values using
Setter Injection.

AppConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

@Bean
public MyBean myBean() {
MyBean myBean = new MyBean();
myBean.setAge(25); // Injecting primitive value
myBean.setName("John Doe"); // Injecting string value
return myBean;
}
}

MyBean.java
public class MyBean {
private int age;
private String name;

// Setter methods
public void setAge(int age) {
this.age = age;
}

public void setName(String name) {


this.name = name;
}

// Getter methods
public int getAge() {
return age;
}

public String getName() {


return name;
}
}

MainApp.java
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {


public static void main(String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);

// Retrieve the bean from the Spring container


MyBean myBean = context.getBean(MyBean.class);

// Accessing values
System.out.println("Name: " + myBean.getName());
System.out.println("Age: " + myBean.getAge());

context.close();
}
}

3. Design and develop JSP application to demonstrate

1. JSP Scripting elements


<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ISP Application</title>
</head>
<body>
<h1>Welcome to ISP Application!</h1>
<%
String companyName = "AwesomeISP";
out.println("Company Name: " + companyName);
%>
</body>
</html>

2. JSP Directives
<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ISP Application</title>
</head>
<body>
<h1>Welcome to ISP Application!</h1>
<%@ include file="header.jsp" %>
<c:set var="companyName" value="AwesomeISP" />
<p>Company Name: ${companyName}</p>
</body>
</html>

<!-- header.jsp -->


<h2>This is header included using JSP Directive</h2>

3. JSP Implicit Objects


<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ISP Application</title>
</head>
<body>
<h1>Welcome to ISP Application!</h1>
<%
String companyName = "AwesomeISP";
out.println("Company Name: " + companyName);
%>
<p>Server Time: <%= new java.util.Date() %></p>
</body>
</html>

4. JSP Action tags


<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ISP Application</title>
</head>
<body>
<h1>Welcome to ISP Application!</h1>
<jsp:useBean id="customer" class="com.example.Customer" scope="request" />
<jsp:setProperty name="customer" property="name" value="John Doe" />
<jsp:setProperty name="customer" property="email" value="[email protected]" />
<p>Customer Info: ${customer.name}, ${customer.email}</p>
</body>
</html>

<!-- index.jsp -->


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ISP Application</title>
</head>
<body>
<h1>Welcome to ISP Application!</h1>
<jsp:useBean id="customer" class="com.example.Customer" scope="request" />
<jsp:setProperty name="customer" property="name" value="John Doe" />
<jsp:setProperty name="customer" property="email" value="[email protected]" />
<p>Customer Info: ${customer.name}, ${customer.email}</p>
</body>
</html>

4. Write a JSP program to validate username and password


<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ISP Login</title>
</head>
<body>
<h1>ISP Login</h1>
<form action="login.jsp" method="post">
Username: <input type="text" name="username" required><br>
Password: <input type="password" name="password" required><br>
<input type="submit" value="Login">
</form>
</body>
</html>

<!-- login.jsp -->


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.HashMap" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Result</title>
</head>
<body>
<h1>Login Result</h1>
<%
// Hardcoded username and password for demo purposes
String validUsername = "user";
String validPassword = "password";

// Retrieve username and password from the request


String username = request.getParameter("username");
String password = request.getParameter("password");

// Perform validation
if (validUsername.equals(username) && validPassword.equals(password)) {
out.println("<p>Login successful!</p>");
} else {
out.println("<p>Login failed. Invalid username or password.</p>");
}
%>
</body>
</html>
l>

You might also like