Database Access using Struts2,Eclipse and Oracle

In this Tutorial we are comparing login page values with database table(login)'s values and if entries matched then transferring login page to success page else page will return back.
Project View :


login.jsp
This is page for user's input and here we using struts tags and on submit action is login.
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
 <%@ taglib prefix="s" uri="/struts-tags" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Login page</title>  
 </head>  
 <body>  
 <h1>Login</h1>  
 <s:form action="login">  
 <s:textfield label="UserName:" name="uname"/>  
 <s:password label="Password:" name="upass"/>  
 <s:submit value="Login"/>  
 </s:form>  
 </body>  
 </html>  

struts.xml
This file contains information about which action class to be invoked. here we are invoking DbAction class using action name login present in login.jsp's page form tag on submit button click.
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE struts PUBLIC  
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
   "http://struts.apache.org/dtds/struts-2.0.dtd">  
   <struts>  
   <package name="log" namespace="/" extends="struts-default">  
   <action name="login" class="com.webideaworld.blog.DbAction" method="execute">  
   <result name="success">/user.jsp</result>  
   <result name="error">/login.jsp</result>  
   </action>  
   </package>  
   </struts>  

web.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  <display-name>ProjDBAccess</display-name>  
  <welcome-file-list>  
     <welcome-file>login.jsp</welcome-file>  
  </welcome-file-list>  
  <filter>  
    <filter-name>struts2</filter-name>  
    <filter-class>  
      org.apache.struts2.dispatcher.FilterDispatcher  
    </filter-class>  
   </filter>  
   <filter-mapping>  
    <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
   </filter-mapping>  
 </web-app>  

DbAction.java
This is a POJO class and it is working as action class for Struts.
 package com.webideaworld.blog;  
 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.ResultSet;  
 import java.sql.Statement;  
 import com.opensymphony.xwork2.ActionSupport;  
 @SuppressWarnings("serial")  
 public class DbAction extends ActionSupport {  
      private String upass;  
      private String uname;  
      private String name;  
      public String execute() throws Exception{  
                 Class.forName("oracle.jdbc.driver.OracleDriver");  
                 Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");  
            Statement st=conn.createStatement();  
            ResultSet rs=st.executeQuery("select * from login where uname='"+uname+"' and upass='"+upass+"'");  
            if(rs.next())  
            {  
                 name=rs.getString(2);  
                 //System.out.println(rs.getString(1));  
                 return SUCCESS;  
            }  
            else  
            {  
                 return ERROR;  
            }  
      }  
      public String getUpass() {  
           return upass;  
      }  
      public void setUpass(String upass) {  
           this.upass = upass;  
      }  
      public String getName() {  
           return name;  
      }  
      public void setName(String name) {  
           this.name = name;  
      }  
      public String getUname() {  
           return uname;  
      }  
      public void setUname(String uname) {  
           this.uname = uname;  
      }  
 }  

user.jsp
This is a success page that displays hello message with User Name.
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
   <%@ taglib prefix="s" uri="/struts-tags" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 </head>  
 <body>  
 hello <s:property value="uname"/>  
 </body>  
 </html>  


Output:


More Struts Topics :

No comments: