Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Struts Error

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am new to Struts. when i try to get collection value from Action Class to my next jsp page i got that bellow error:
javax.servlet.ServletException: Cannot find bean loginForm in any scope
Any body please help me, i give my code also

Login.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title><bean:message key="login.title"/></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="-1">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<html:form action="register.do" method="post" focus="login">
<table border="0">
<tr>
<td><bean:message key="login.username"/></td>
<td><html:text property="login"/></td>
</tr>
<tr>
<td><bean:message key="login.password"/></td>
<td><html assword property="password" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit value="Login"/></td>
</tr>
</table>
</html:form>
</body>
</html:html>

LoginForm.java

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import java.util.*;

public class LoginForm extends ActionForm {
private String password;
private String login;
private Vector vect;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public String getLogin() {
return login;
}

public void setLogin(String login) {
this.login = login;
}

public Vector getVect() {
return vect;
}

public void setVect(Vector vect) {
this.vect = vect;
}
}

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/


import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.Globals;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.DataSource;
import java.util.Vector;


LoginAction.java

public class LoginAction extends Action {
Connection con = null;
Statement st = null;
ResultSet rs = null;
boolean result = false;
Vector vec = new Vector();
boolean user ;

public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {

LoginForm lf = (LoginForm)form;
HttpSession session = request.getSession();
String username = lf.getLogin();
String password = lf.getPassword();

DataSource dataSource = (DataSource) servlet.getServletContext().getAttribute(Globals.DATA_SOURCE_KEY);
try{
con = dataSource.getConnection();
st = con.createStatement();
if(username.length()>0 && password.length()>0){
login log = new login(st);
user=log.userChecking(username,password);
if(user)
vec = log.getRecords();
lf.setVect(vec);
return mapping.findForward("success");
}
}catch(Exception e){
System.out.println("Error "+e);
}
return mapping.findForward("failure");
}
}

login.java

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionMapping;
import javax.sql.DataSource;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class login {
ResultSet rs = null;
ActionMapping mapping=null;
Vector vec = new Vector();
boolean user =false;

login(){
}

public login(Statement st){
this.st = st;
}

public boolean userChecking(String username,String password){
try{
String query = "select username,password from tbluser where username='"+username+"' and password='"+password+"'";
rs = st.executeQuery(query);
if(rs.next()){
user = true;
return user;
}

}catch(Exception e){
System.out.println("Error "+e);
}
return user;
}
public Vector getRecords(){
LoginAction la = new LoginAction();
try{
String query1 = "select * from tbldata";
rs = st.executeQuery(query1);
while(rs.next()){
la.setName(rs.getString("name"));
la.setAddress(rs.getString("address"));
la.setPhone(rs.getInt("phoneno"));
la.setDt(rs.getString("date"));
vec.addElement(la);
}
}catch(Exception e){
System.out.println("Error "+e);
}
return vec;
}
}

data.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/display.tld" prefix="display" %>
<html:html>
<head>
<title><bean:message key="login.title" />
</title>
</head>
<body>
<table border="1">
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone No</th>
<th>Date</th>
</tr>
<logic:iterate id="result" name="loginForm" property="vect">
<tr>
<td><bean:write name="result" property="name"/></td>
<td><bean:write name="result" property="address"/></td>
<td><bean:write name="result" property="phone"/></td>
<td><bean:write name="result" property="dt"/></td>
</tr>
</logic:iterate>
</table>
</body>
</html:html>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
<data-sources >
<data-source key="org.apache.struts.action.DATA_SOURCE">
<set-property property="password" value="" />
<set-property property="minCount" value="1" />
<set-property property="maxCount" value="4" />
<set-property property="user" value="" />
<set-property property="driverClass" value="sun.jdbc.odbc.JdbcOdbcDriver" />
<set-property property="description" value="Struts Test" />
<set-property property="url" value="Jdbc dbc river={Microsoft Access Driver (*.mdb)};DBQ=C:\Program Files\Apache Tomcat 4.0\webapps\ROOT\Struts\StrutsDB.mdb" />
<set-property property="readOnly" value="false" />
<set-property property="autoCommit" value="false" />
<set-property property="loginTimeout" value="" />
</data-source>
</data-sources>

<form-beans >
<form-bean name="loginForm" type="LoginForm" />
</form-beans >
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="LoginForm" name="loginForm" path="/register" scope="request" type="LoginAction">
<forward name="success" path="/data.jsp" redirect="true" />
<forward name="failure" path="/Failure.html" redirect="true" />
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>


 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai try this
Add vector into session using session.setAttribute("vec",vec);
 
Gopnan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for you reply.
I put vector values into session. I got values in my next page(data.jsp page).But it returned last value of the table only with record iteration .
How can i get each record of the table. Pls give a solution
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The issue that I see is the redirect="true" setting on your forwards. This will cause all values on your request to go away. I typically set redirect to true when forwarding to another action but I cannot think of a reason to do this if forwarding to a jsp.

- Brent
 
Gopnan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I remove that redirect in my struts-config but it also return same value.
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic