Menu
I need to write a class file that can be re-usable throughout our application. What I need to do is create a class file that when a user enters in some data, and that data is validated against our database, if the data is invalid (wrong pw, invalid userid, etc), my class creates a cookie that is just a counter and implents by one each time the user gets an error, and once the user gets 3 errors, they are redirected to a customer service page. We are using J2EE and using IBM’s WebSphere.
Thanks for any help!!!
<i>
</i>public ErrorCounter
{
private int errorCount = 0;
ErrorCounter ErrorCounter()
{
super();
}
int getErrorCount()
{
return errorCount;
}
void countUp()
{
errorCount++;
}
}
<i>
</i>public ErrorCounter
{
public ErrorCounter()
{
count = 0;
instance = this;
}
<i> </i>public static ErrorCounter getInstance()
<i> </i>{
<i> </i> return instance;
<i> </i>}
<i> </i>public int getErrorCount()
<i> </i>{
<i> </i> return count;
<i> </i>}
<i> </i>public void addError()
<i> </i>{
<i> </i> count++;
<i> </i>}
<i> </i>private int count;
<i> </i>private static ErrorCounter instance;
}
<i>
</i>ErrorCounter counter = ErrorCounter.getInstance();
[i]Originally posted by jrthor2 [/i]The security stuff is way too much to be explained in a forum like this. I'd recommend you get cozy with the WAS Infocenter and the IBM Redbooks site.
[B]We are lookig at container manages security. How would I do this using that instead of this way?
Also, in your code example, how do I redirect accordingly? [/B][/QUOTE]
<i>
</i>package handling;
// File is in root/handling
<i>
</i>import handling.*;
//or
import handling.ErrorCounter
[i]Originally posted by jrthor2 [/i]
[B]We are using the Struts framework [/B][/QUOTE]
<i>
</i>package webproject.actions;
import java.util.Collection;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.util.ArrayList;
import java.util.HashMap;
import webproject.forms.LoginFormBean;
import webproject.util.beans.NavBean;
/**
* @version 1.0
* @author
*/
public class LoginActionAction extends Action {
<i> </i>public ActionForward execute(
<i> </i> ActionMapping mapping,
<i> </i> ActionForm form,
<i> </i> HttpServletRequest request,
<i> </i> HttpServletResponse response)
<i> </i> throws Exception {
<i> </i> ActionErrors errors = new ActionErrors();
<i> </i> ActionForward forward = new ActionForward();
<i> </i> // return value
<i> </i> LoginFormBean loginFormBean = (LoginFormBean) form;
<i> </i> int attemptsCtr = 0;
<i> </i> try {
<i> </i> request.setAttribute("loginFormBean", loginFormBean);
<i> </i> } catch (Exception e) {
<i> </i> // Report the error using the appropriate name and ID.
<i> </i> errors.add("name", new ActionError("id"));
<i> </i> }
<i> </i> // If a message is required, save the specified key(s)
<i> </i> // into the request for use by the <struts:errors> tag.
<i> </i> if (!errors.isEmpty()) {
<i> </i> saveErrors(request, errors);
<i> </i> }
<i> </i> // Write logic determining how the user should be forwarded.
<i> </i> request.getParameter("attemptsCtr");
<i> </i> if (attemptsCtr < 3) {
<i> </i> attemptsCtr++;
<i> </i> request.setParameter("attemptsCtr",String.valueOf(attemptsCtr ));
<i> </i> } Else {
<i> </i> forward = mapping.findForward("<cust_serv>");
<i> </i> }
<i> </i> forward = mapping.findForward("<success>");
<i> </i> // Finish with
<i> </i> return (forward);
<i> </i>}
}
[i]Originally posted by jrthor2 [/i]If you put the object into the session then it will be available for any other object running under that session. It can be incremented and tested from any JSP or other servlet or Struts action.
[B]Ok, but that would be specific to logining in, right? I want it to be pretty "modular" where I can use this on other pages too, that might have different fields for user input. [/B][/QUOTE]
}
What is wrong with the code I have so far?[/QUOTE]
} Else {
Do you mean the angle brakets in the forward mapping statements?[/QUOTE]Yes
What do you mean put the object into the session, how do I do that?[/QUOTE]:rolleyes: That's scary. You REALLY need to learn the basics before you start writing code. The session is an object associated by the container with the user's long term (multi-request) interaction with the web container. You store things in there that should persist across all the HTTP transactions that happen during that user's "session." Since you will have at least three HTTP transactions causing your example failure, the associated information must be kept in the session.
first of all putting anything in sessions should be an absolute last thing to do.[/QUOTE]Things should go where they need to go. The Session is the right place for session things that ought to be cached relative to a session, e.g. an object representing the user of the session, a collection of shopping cart items, objects supporting transactional integrity, etc.
[i]Originally posted by ray326 [/i]
[B]Things should go where they need to go. [/B][/QUOTE]
<i>
</i>package us.test.webproject.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @author
*
* To change the template for this generated type comment go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
public class ErrorCounterAction extends Action {
<i> </i>public ActionForward execute(
<i> </i> ActionMapping mapping,
<i> </i> ActionForm form,
<i> </i> HttpServletRequest request,
<i> </i> HttpServletResponse response)
<i> </i> throws Exception {
<i> </i> String paramAttempts = request.getParameter("attemptsCtr");
<i> </i> if(paramAttempts!=null && !paramAttempts.equals("")){
<i> </i> try{
<i> </i> attemptsCtr = Integer.parseInt(paramAttempts);
<i> </i> }catch(NumberFormatException nfe){
<i> </i> // put some logic here
<i> </i> }
<i> </i> }
<i> </i> // now you have value in the attemptsCtr
<i> </i> if (attemptsCtr < 3) {
<i> </i> attemptsCtr++;
<i> </i> request.setParameter("attemptsCtr",String.valueOf(attemptsCtr ));
<i> </i> } Else {
<i> </i> forward = mapping.findForward("<cust_serv>");
<i> </i> }
<i> </i> // Finish with
<i> </i> return (forward);
<i> </i>}
}
[i]Originally posted by jrthor2 [/i]It's not AFTER the "else" it IS the "Else"!
[B]I get a syntax error and it points to the { after the else. [/B][/QUOTE]
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
[b]int attemptsCtr= 0;//do not declare this valriable as a global variable.[/b]
String paramAttempts = request.getParameter("attemptsCtr");
if(paramAttempts!=null && !paramAttempts.equals("")){
try{
attemptsCtr = Integer.parseInt(paramAttempts);
}catch(NumberFormatException nfe){
// put some logic here
}
}
// now you have value in the attemptsCtr
if (attemptsCtr < 3) {
attemptsCtr++;
request.setParameter("attemptsCtr",String.valueOf(attemptsCtr ));
[b]} else {[/b]//Else is used in VB and family of languages
forward = mapping.findForward("<cust_serv>");
}
// Finish with
return (forward);
}
<i>
</i>ErrorCounterAction errCount = new us.riteaid.crm_v1webproject.actions.ErrorCounterAction();
request.setAttribute("attemptsCtr", errCount);
Also, what is wrong with the else part? If I make this "Else" instead of "else", I get a syntax error:[/QUOTE]Ok this is the THIRD time I've answered this one. Maybe if I type slower and in bold.
0.1.9 — BETA 6.18