0

This is my code I had written a logic to fetch the username and password from and database and after login the login time and logout time should enter into the database the time should be taken from the system

action class:

public class Login {
    private String username;
    private String password;
    private String login_time;
    private String logout_time;
    private String status;
    private String late;
    //getters and setters

 @SuppressWarnings("unused")
 public String execute(ServletRequest req) throws Exception,SQLException{
int i=0;
    try{
        SessionUtils su = new SessionUtils(); 
        HttpSession session  =((Request) req).getSession();
        String hql="select * from login where username='"+username+"'and      
password='"+password+"'";           
        
        Query query1=((SessionUtils) session).createQuery(hql);
        query1.setParameter(1,getUsername());
        query1.setParameter(2,getPassword());
        
        int result = query1.executeUpdate();
        while(i!=0)
        {
            RequestDispatcher rd 
 =req.getRequestDispatcher("DailyInOut.jsp");
            return  hql;
        }
         } catch (Exception e) 
         {
             return late;
         }
    return status;
   }

  public String chandu(ServletRequest req,ServletResponse res) throws      
  Exception,SQLException{
int j=0;
try
{
    java.util.Date myDate = new java.util.Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
    String strDates1 = formatter.format(myDate);
    @SuppressWarnings("unused")
    SessionUtils su = new SessionUtils(); 
    String hql="update attendance  where date='"+strDates1+"' name='"+username+"' 
   login='"+login_time+"' logout='"+logout_time+"' status='"+status+"' 
 late='"+late+"'";
    
    HttpSession session  =((Request) req).getSession();
    Query query2=((SessionUtils) session).createQuery(hql);
    session.setAttribute("date", strDates1);
    session.setAttribute("login_time",login_time);
    session.setAttribute("logout_time",logout_time);
    session.setAttribute("status",status);
    session.setAttribute("late",late);
    @SuppressWarnings("unused")
    int result = query2.executeUpdate();
    System.out.println(" update row updated");
    if(j!=0)
    {
    System.out.println("success1");
    return "success";
}
}
catch(Exception e)
{
    e.getMessage();
}
System.out.println("failure page");
return "failure";
}
}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">

    <struts>
        <package namespace="/" name="packageOne" extends="struts-default">
            <action name="login" class="com.tribro.chandu.Login" method="post">
                <result name="success">DailyInOut.jsp</result>
                </action>
           </package>
    </struts>

logina.jsp

    <html>
   <head>
  <title>Home Page</title>
  </head>
  <body bgcolor="cyan" text="magenta">
  <form action="login" method="execute">
  <img src="WebContent/Images/java.jpg" height="80"/>
   <pre><marquee behavior="scroll" direction="right"><font size="4">Welcome to TRIBRO     
 Limited</font></marquee><br/></pre>
  <table>
  <tr>
  <td>
  <div>
  <img src="WebContent/Images/banner.png" height="300" width="900"></img>
  </div>
  </td>
  <td>
  <div>
 <table>
 <tr>
 <td colspan="2" style="text-align:center;">
 <h4>Login Form</h4>
 </td>
 </tr>
 <tr>
 <td>
 <h4>Username :</h4>
 </td>
 <td>
 <h4><input type="text" name="Username"/></h4>
  </td>
 </tr>
  <tr>
 <td>
 <h4>password:</h4>
 </td>
 <td>
 <h4><input type="password" name="password"/></h4>
 </td>
 </tr>
 <tr>
  <td colspan="2" style="text-align:center;">
 <input type="submit" value="Login"/>
 </td>
 </tr>
 </table>
  </div>
  </td>
  </tr>
 </table>
 </form>
 </body>
 </html>

But I am getting an error as

java.lang.NoSuchMethodException: com.tribro.chandu.Login.post()
Roman C
  • 49,761
  • 33
  • 66
  • 176
user3465058
  • 41
  • 1
  • 7

2 Answers2

0

When you submit a form it has an incorrect method

<form action="login" method="execute">

In the action mapping you have configured the method post() will be mapped to the login action. But, your action class doesn't have such method. If you change this mapping to

<action name="login" class="com.tribro.chandu.Login"> 

this will map to the execute() method by default. This method you should create in the action class. It has different signature which has not any parameters.

If you need to know how to get servlet objects such as HttpServletRequest then you can read this answer.

You can get the request from the action context like

HttpServletRequest request = ServletActionContext.getRequest();

That way is useful in interceptors, but in action better to implement ServletRequestAware

protected HttpServletRequest request;

public void setServletRequest(HttpServletRequest request) {
 this.request = request;
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • sir,thank you but i used this before if i used this tag i am getting the Exception as java.lang.NoSuchMethodException: com.tribro.chandu.Login.execute() – user3465058 May 27 '14 at 08:14
  • What is the name of the class you have posted, isn't it a `Login` class? – Roman C May 27 '14 at 08:25
  • sir my class name is Login class only – user3465058 May 27 '14 at 09:03
  • sir,actually my need to compare the username and password from the values in the database if login success then the login time logout time and by how much the employee is late to login and this must be placed in the database of attendance but i am getting 500 eroors from 4 days – user3465058 May 27 '14 at 09:06
  • There are many ways to do that you can check the online tutorials or [this](http://stackoverflow.com/a/17898392/573032) answer. – Roman C May 27 '14 at 09:21
0

but I am getting java.lang.NoSuchMethodException: com.tribro.chandu.Login.post()

  • This makes me think you have written method="post" a <s:submit/> tag (or in struts.xml):

    <s:form action="login">
        <s:submit method="post" />
    </s:form>
    

    where method refers to an Action method, not to an HTTP method (like in <s:form />).
    Then remove method from the submit tag, and leave it on the form:

    <s:form action="login" method="post">
        <s:submit />
    </s:form>
    

if i used this tag i am getting the Exception as java.lang.NoSuchMethodException: com.tribro.chandu.Login.execute()

  • You are putting parameters in your Action methods:

    public String execute(ServletRequest req) throws Exception,SQLException{
    

    this is not how it works, Action methods have no arguments, the parameters are passed through getters and setters, including ServletRequest and ServletResponse (read more here).

    Then rewrite your methods as:

    public String execute() throws Exception{
    

    and they will be found by Struts.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243