Sunday, April 26, 2020
Read value from properties file
To get the value of database connection or fetch labels for JSP, it is better to use properties file. So that if you change at one place, it will be reflected to all places.
Code to read properties file and return value
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReadLabelProperties {
public static String getProperties(String key) throws IOException
{
InputStream inputStream=ReadLabelProperties.class.getClassLoader().getResourceAsStream("label.properties");
Properties myproperties = new Properties();
myproperties.load(inputStream);
return myproperties.getProperty(key);
}
}
label.properties are having entries like below
dbserver=jdbc:mysql://localhost:3306/
dbdriver=com.mysql.jdbc.Driver
Hope the same is useful while creating web application
Deepak
Monday, April 13, 2020
Web Application
After a long time, I got chance to design a web application:
Dynamic web application, using JSP and Servlet
-Download Java (JDK and JRE) and install
-Download Eclipse IDE and install
-Download Tomcat and install
-Download MySql and install
- File - Download - Dynamic Web Project
Give Project Name-(Next)-src-(Next)-Click on web.xml (deployment descriptor)
web.xml: (file would be like this
WebApp1
login.jsp
Login
LoginServlet
LoginServlet
/login/*
- login.jsp
-loginBean.jsp
Class:
1- LoginBean
public class LoginBean {
String userName="";
String password="";
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}2- LoginServlet
import javax.servlet.annotation.WebServlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.sql.*;
/**
* Servlet implementation class Login
*/
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Server at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username=request.getParameter("username").toString();
String userpass=request.getParameter("password").toString();
HttpSession session = request.getSession(true);
if(username.equalsIgnoreCase(userpass))
{
response.sendRedirect("welcome.jsp");
}
else
{
response.sendRedirect("login.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
- Select Project-Run on Server
Subscribe to:
Posts (Atom)