1

I have this error when i started login.jsp form and type a username and password The error was HTTP STATUS 404: The requested resource is not available.

What am i missing? any help?

LoginAction.java

package com.tutorialspoint.struts2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

 private String user;
 private String password;
 private String name;

public String execute() {
  String ret = ERROR;
  Connection conn = null;

  try {
     String URL = "jdbc:mysql://localhost/struts_tutorial";
     Class.forName("com.mysql.jdbc.Driver");
     conn = DriverManager.getConnection(URL, "root", "");
     String sql = "SELECT name FROM login WHERE";
     sql+=" user = ? AND password = ?";
     PreparedStatement ps = conn.prepareStatement(sql);
     ps.setString(1, user);
     ps.setString(2, password);
     ResultSet rs = ps.executeQuery();

     while (rs.next()) {
        name = rs.getString(1);
        ret = SUCCESS;
     }
  } catch (Exception e) {
     ret = ERROR;
  } finally {
     if (conn != null) {
        try {
           conn.close();
        } catch (Exception e) {
        }
     }
  }
  return ret;
 }

public String getUser() {
  return user;
 }

public void setUser(String user) {
  this.user = user;
 }

public String getPassword() {
  return password;
 }

public void setPassword(String password) {
  this.password = password;
 }

public String getName() {
  return name;
 }

public void setName(String name) {
  this.name = name;
 }
}

Login.jsp

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Login Example</title>
 </head>
    <body>
    <html:form action="/login" focus="userName">
      Username : <html:text property="userName" />
    <br>
      Password : <html:password property="password" />
    <br>
     <html:submit value="login" />
    </html:form>

    </body>
</html>

Web.xml

<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_3_0.xsd"
id="WebApp_ID" version="3.0">

<display-name>Struts 2</display-name>
<welcome-file-list>
  <welcome-file>index.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 Configuration

<?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.devMode" value="true" />
  <package name="helloworld" extends="struts-default">

    <action name="loginaction" 
      class="com.tutorialspoint.struts2.LoginAction"
      method="execute">
     <result name="success">/success.jsp</result>
     <result name="error">/error.jsp</result>
    </action>

  </package>

 </struts>

This is what i tried http://www.tutorialspoint.com/struts_2/struts_database_access.htm

Roman C
  • 49,761
  • 33
  • 66
  • 176
user3363577
  • 11
  • 1
  • 4

1 Answers1

1

So many questions these days abount not working examples from tutorialspoint :(

Those tutorials are old, many things have changed.

  1. Change the old FilterDispatcher to the new StrutsPrepareAndExecuteFilter;
  2. Include commons-lang3-3.x.jar in your project;
  3. Change the version of your Servlet API according to a) your servlet-api-x.x.jar and b) your Servlet Container capabilities (eg. you need to ensure your Application Server is suporting Servlet API 3.0 if you want to use them... remember to not include the servelt-api jar in your WAR / EAR): you have a mixed config, specifying both 2.5 and 3.0

    <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_3_0.xsd"
         id="WebApp_ID" version="3.0">
    

    , fix that;

  4. Ensure you have all the required Struts2 libraries, all aligned to the latest version (2.3.16.3 currently), with all their dependences.

Then it should work.

After that, you can start looking for best practices, like using <s: (default prefix for Struts2) instead of <html: (default prefix for Struts1), and connecting to a database from a business/DAO layer instead that from the controller (Action).

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thanks for your help. But problem is same. i think there have some issue with database, when i add username, password and click login button It shows ' The requested resource not available' with some session id. Any idea to fix this problem? – user3363577 Oct 01 '14 at 10:43
  • 1
    You are not telling us which steps of the many suggested here you have taken. Have you changed the filter ? Have you changed the web-app declaration ? Have you checked and eventually aligned all the JARs ? – Andrea Ligios Oct 01 '14 at 12:01
  • Yes, i did all thing u said. but the problem is same. any idea? – user3363577 Oct 01 '14 at 12:30
  • Do you have `<%@ taglib prefix="html" uri="/WEB-INF/struts-tags.tld"%>` in your JSP (as first row) ? – Andrea Ligios Oct 01 '14 at 12:39
  • It is right. But then in your jsp, change **all** the `` to ` ` – Andrea Ligios Oct 01 '14 at 13:24
  • now error shows this, 'The requested resource (/LoginExampleStruts1/loginaction) is not available.' when im going to loging with username and password – user3363577 Oct 01 '14 at 13:25
  • lol... there is another mistake: change `action="/login"` to `action="loginaction"`... why are you calling an action with a different namespace ("/" instead of "") and action name from the ones defined in struts.xml ? Change this, it will work. – Andrea Ligios Oct 01 '14 at 14:02