![]() |
|||||
![]() |
![]() |
![]() |
![]() |
Poniżej znajduje się kod modułu, który będzie logował (lub nie) użytkownika
do serwisu.
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <!-- Konfiguracja dla serwleta action (kontrolera Struts) --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <!-- opis kluczy --> <param-name>application</param-name> <param-value>portal.Resources</param-value> </init-param> </servlet> <!-- servlet Struts-a, ktory pelni funkcje kontrolera --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>/execute/*</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> <struts-config> <form-beans> <form-bean name="zalogujForm" type="portal.forms.ZalogujForm"/> </form-beans> <global-forwards> <forward name="zaloguj" path="/zaloguj.jsp"/> </global-forwards> <action-mappings> <action path="/zaloguj" type="portal.akcje.Zaloguj" name="zalogujForm" input="/zaloguj.jsp" scope="request" validate="true"> <forward name="blad" path="/zaloguj.jsp"/> <forward name="sukces" path="/glowna.jsp"/> </action> </action-mappings> </struts-config>
<%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts.tld" prefix="s" %> <html><body> <html:form action="/zaloguj" focus="id"> <html:errors/> <!-- wypisz bledy --> <s:message key="login.id"/> <!-- wypisz zachete do wpisania identyfikatora --> <html:text property="id" size="16" maxlength="16"/> <s:message key="login.haslo"/> <html:password property="haslo" size="16" maxlength="16" redisplay="false"/> <html:submit property="submit"> <s:message key="login.submit"/> </html:submit> </html:form> </body></html>
package portal.forms; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.*; public class ZalogujForm extends ActionForm { private String id = null; private String haslo = null; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getHaslo() { return haslo; } public void setHaslo(String haslo) { this.haslo = haslo; } public void reset(ActionMapping mapping, HttpServletRequest request) { this.haslo = null; this.id = null; } public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((id == null) || (id.length() == 0)) errors.add("id", new ActionError("error.id.required")); if ((haslo == null) || (haslo.length() == 0)) errors.add("haslo", new ActionError("error.haslo.required")); return errors; } }
package portal.akcje; import javax.servlet.ServletException; import javax.servlet.http.*; import org.apache.struts.action.*; import portal.forms.ZalogujForm; public class Zaloguj extends Action { public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException { ActionErrors errors = new ActionErrors(); /* kolekcja bledow */ String id = ((ZalogujForm) form).getId(); String haslo = ((ZalogujForm) form).getHaslo(); if (!haslo.equals("x")) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.password.mismatch")); saveErrors(request, errors); /* powrot do poprzedniej strony */ return (new ActionForward(mapping.getInput())); } return (mapping.findForward("sukces")); } }
error.password.mismatch=Zle haslo! error.id.required=Brak identyfikatora! error.haslo.required=Brak hasla! login.id=Id: login.haslo=Haslo: login.submit=Zaloguj