Ciao a tutti, MI sono appena iscritto nel forume dopo aver letto regolamento e ricercato prima di postare un thread che mi potev aiutare sono giunto ad aprire questo.
Sto facendo un programma in Java. Ho fatto uso di una servlet per avviare tutto il programma.
Praticamente da una pagina .jsp richiamo la servlet.
La servlet usufruisce di alcune proprietà che ho pensato bene di dichiarare, per modificarle successivamente, nel web.xml.
Il web.xml è questo
La servlet mi parte, il problema è che l'immagine PAR.jpg che dovrebbe generare non mi presenta le proprietà che ho inizializzato nel web.xml. Ovvero non mi carica le proprietà come: noise_color, noise_font_color, text_creator etc.codice:<?xml version="1.0" encoding="UTF-8"?> <!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 id="WebApp"> <servlet> <servlet-name>PARServlet</servlet-name> <display-name>PAR</display-name> <servlet-class>PAR.PARServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <init-param> <param-name>pwd_length</param-name> <param-value>6</param-value> </init-param> <init-param> <param-name>noise</param-name> <param-value>yes</param-value> </init-param> <init-param> <param-name>noise_color</param-name> <param-value>166,31,255</param-value> </init-param> <init-param> <param-name>grid_background</param-name> <param-value>no</param-value> </init-param> <init-param> <param-name>background</param-name> <param-value></param-value> </init-param> <init-param> <param-name>grad_background_to</param-name> <param-value>255,255,255</param-value> </init-param> <init-param> <param-name>grad_background_from</param-name> <param-value>75,75,255</param-value> </init-param> <init-param> <param-name>text_creator</param-name> <param-value>FiveLetterFirstName</param-value> </init-param> ' <init-param> <param-name>distortion</param-name> <param-value>WaterRipple</param-value> </init-param> <init-param> <param-name>font_color</param-name> <param-value>16,31,215</param-value> </init-param> <servlet-mapping> <servlet-name>PARServlet</servlet-name> <url-pattern>/PAR.jpg</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>SimpleCapchaExample.jsp</welcome-file> </welcome-file-list> </web-app>
Il codice della servlet ho provato ad implementarlo in due modi, così, con l'uso di getinitParamaterNames()
e con l'uso di getInitParameter() in questo modocodice:package PAR; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; import javax.servlet.Servlet; import javax.servlet.ServletConfig; //import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class PARServlet extends HttpServlet implements Servlet { private static final long serialVersionUID = 1L; protected final static String PAR_SESSION_KEY="PAR_SESSION_KEY"; //la variabile in cui verrà salvata la chiave di generazione immagine //le variabili che assumono i valori dal web.xml protected final static String PWD_LENGTH="pwd_length";//lunghezza della pwd visualizzata protected final static String NOISE="noise";//attivazione della linea di disturbo protected final static String NOISE_COLOR="noise_color";//colore della linea protected final static String GRID_BACKGROUND="grid_background"; protected final static String BACKGROUND="background";//sfondo monocolore protected final static String GRAD_BACKGROUND_TO="grad_background_to";//sfondo graduato colore 1 protected final static String GRAD_BACKGROUND_FROM="grad_background_from";//sfondo graduato colore 2 protected final static String TEXT_CREATOR="text_creator";//tipo di creazione pwd protected final static String DISTORTION="distortion";//tipo di algoritmo di distorsione protected final static String FONT_COLOR="font_color";//il colore della password private Properties props=null; private TextCreator text;//si istanzia l'oggetto che genererà la password della PAR private PARProducer par;//si istanzia l'oggetto che creerà la PAR private String pwd; public void init(ServletConfig conf) throws ServletException { super.init(conf); /*props = new Properties(); Enumeration e = conf.getInitParameterNames(); while (e.hasMoreElements()) { String key=(String)e.nextElement(); String value=conf.getInitParameter(key); props.put(key,value); } } public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException { par=new PARProducer(props); //crea la password: o di tipo FiveLetterFirstName o random a seconda del param nel web.xml if (props.containsKey(TEXT_CREATOR)) { text=new TextCreator(props); String producer=props.getProperty(TEXT_CREATOR); if (producer.equals("FiveLetterFirstName")) { try { pwd=text.getFiveLetterFirstName(); }catch (Exception ex) {} } else { pwd=text.getText(); } } else { text=new TextCreator(props); pwd=text.getText(); } request.getSession().setAttribute(PAR_SESSION_KEY,pwd); //la pwd viene memorizzata nella PAR_SESSION_KEY par.imageCreator(response.getOutputStream(),pwd); } }
poi ci sono altre otto classi ognuna delle quali riusa queste proprietà...codice:package PAR; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; import javax.servlet.Servlet; import javax.servlet.ServletConfig; //import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class PARServlet extends HttpServlet implements Servlet { private static final long serialVersionUID = 1L; protected final static String PAR_SESSION_KEY="PAR_SESSION_KEY"; //la variabile in cui verrà salvata la chiave di generazione immagine private Properties props=null; private TextCreator text;//si istanzia l'oggetto che genererà la password della PAR private PARProducer par;//si istanzia l'oggetto che creerà la PAR private String pwd; public static String text_creator; static String pwd_length; static String noise; static String noise_color; static String grid_background; static String background; static String grad_background_to; static String grad_background_from; static String distortion; static String font_color; public void init(ServletConfig conf) throws ServletException { super.init(conf); text_creator=conf.getInitParameter("text_creator"); pwd_length=conf.getInitParameter("pwd_length"); noise=conf.getInitParameter("noise"); noise_color=conf.getInitParameter("noise_color"); grid_background=conf.getInitParameter("grid_background"); background=conf.getInitParameter("background"); grad_background_to=conf.getInitParameter("grad_background_to"); grad_background_from=conf.getInitParameter("grad_background_from"); distortion=conf.getInitParameter("distortion"); font_color=conf.getInitParameter("font_color"); } public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException { par=new PARProducer(props); //crea la password: o di tipo FiveLetterFirstName o random a seconda del param nel web.xml if (text_creator!=null) { text=new TextCreator(props); String producer=props.getProperty(text_creator); if (producer.equals("FiveLetterFirstName")) { try { pwd=text.getFiveLetterFirstName(); }catch (Exception ex) {} } else { pwd=text.getText(); } } else { text=new TextCreator(props); pwd=text.getText(); } request.getSession().setAttribute(PAR_SESSION_KEY,pwd); //la pwd viene memorizzata nella PAR_SESSION_KEY par.imageCreator(response.getOutputStream(),pwd); } }
Il problema secono me è che: o web.xml ha qualceh errore all'interno, tipo non ho inizializzato bene qualche cosa, oppure ho usato male i metodi per prendere i valori dei parametri del web.xml.
Secondo voi?
Grazie mille
cyb![]()

Rispondi quotando
