Wednesday 20 February 2013

Login Example


Login Example is works as Hello World example here are some changes in Action file and JSP file and struts.xml.

Web.xml
         The program starts from web.xml file. The web.xml file needs to be created under the WEB-INF folder under WebContent. Eclipse had already created a skeleton web.xml file for you when you created the project. So, let’s just modify it as follows:

Code-

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
      xmlns="http://java.sun.com/xml/ns/j2ee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <display-name>Demo</display-name>
   <welcome-file-list>
    <welcome-file>Hello.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>

Struts.xml
 create struts.xml file under the src  folder.The action is mapped in strus.xml. Here the hello action is mapped and HelloAction class is execute the execute() method. From HelloAction  string “success” is return,then it mapped with result name and success.jsp is executed.

Code-

<?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>
      <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
      <constant name="struts.devMode" value="false"/>
                 
      <package name="default" extends="struts-default" namespace="/">
         <action name="log" class="view.LoginAction" method="execute">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
         </action>
      </package>
</struts>

LoginAction.java
 The flow is coming from the struts.xml file and execute method is executed. The string “success” is return to struts.xml file. 

package view;

import com.opensymphony.xwork2.ActionSupport;


public class HelloAction
{
   public String user;
   public String pass;
  

    public String getUser()
    {
      return user;
     }
    public void setUser(String user)
    {
      this.user = user;
    }
    public String getPass()
    {
      return pass;
    }
    public void setPass(String pass)
    {
      this.pass = pass;
    }


      public String execute()
     {
            if(getUser().equals("admin") && getPass().equals("admin"))
            {
               return "success";
            }
            else
               return "error";
     }
     
}
  
Login.jsp
We also need to create Hello.jsp in the WebContent folder. This file will serve as the initial action URL

Code-

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
   
<!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</title>
</head>
<body>
<s:form name="log" action="hello" method="post">
   <s:textfield name="user" size="20" label="User Name"/><br>
   <s:password name="pass" size="20" label="Password"/><br>
   <s:submit name="submit" value="OK"/>
</s:form>
</body>
</html>


success.jsp
In success.jsp the final message is display.

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Hello</title>
</head>
<body>
   
    Hello <s:property value="user"/><br/>
   
</body>
</html>

error.jsp
In error.jsp the final message is display.

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Hello</title>
</head>
<body>
   
    You are not authorized <s:property value="user"/><br/>
   
</body>
</html>


Output:-


 
  When user enter correct user name then success.jsp is executed.



 When user enter Incorrect user name then error.jsp is executed.



1 comment :

    Categories

    Text Widget

    Followers