Project Structure
Step 1: Create a Web Application using netbean IDE
Step 2: Create 3 packages namely
2.a. com.mybean
2.b. com.resources
2.c. com.dao
Step 3: Create a Class Named MyBean under com.mybean package
package com.mybean;
/**
*
* @author Aravind Sankaran
*/
public class MyBean {
private String firstName;
private String lastName;
private boolean valid;
public boolean isValid() {
return valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Step 4: Create a properties Named config under com.resources package
4.a. Right click com.resources
4.b. New
4.c. properties file
paste below text on config.properties file
# To change this template, choose Tools | Templates
# and open the template in the editor.
firstname=aravind
lastname=sankaran nair
Step 5: Create a Class Named LoadMyProperties under com.dao package
paste the below text
package com.dao;
import com.mybean.MyBean;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class LoadMyProperties {
private Properties configProp = new Properties();
public MyBean getMyPropertiesFile(){
MyBean myBean= new MyBean();
InputStream in = this.getClass().getClassLoader().getResourceAsStream("com/resources /config.properties");
try {
configProp.load(in);
String firstname=configProp.getProperty("firstname");
String lastname=configProp.getProperty("lastname");
myBean.setFirstName(firstname);
myBean.setLastName(lastname);
myBean.setValid(true);
} catch (IOException e) {
e.printStackTrace();
}
return myBean;
}
}
Step 5: Paste the below code in index.jsp file
<%--
Document : index
Created on : 19 Jun, 2013, 7:07:36 PM
Author : Aravind Sankaran
--%>
<%@page import="com.mybean.MyBean"%>
<%@page import="com.dao.LoadMyProperties"%>
<%@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>
<%!String myFirstName;%>
<%!String myLastName;%>
<%
MyBean myBean=new MyBean();
LoadMyProperties loadMyProperties=new LoadMyProperties();
myBean=loadMyProperties.getMyPropertiesFile();%>
<% if (myBean.isValid()) {
myFirstName=myBean.getFirstName();
myLastName=myBean.getLastName();
%>
<% } else { %>
<p> No data in properties file</p>
<% } %>
welcome <%=myFirstName%> <%=myLastName%>
</body>
</html>
Output:
welcome aravind sankaran nair
No comments:
Post a Comment