|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: May 2003
Messaggi: 1534
|
Servlet e String
Ho scritto questa servlet stupida:
import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class HitServlet extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException { String message = ""; message=cerca_news(request.getParameter("user")); System.out.println(message); response.setContentType("text/plain"); response.setContentLength(message.length()); PrintWriter out = response.getWriter(); out.println("Hai queste news per te:"+message); } String cerca_news(String User) { String user=""; String news=""; try { BufferedReader inputStream = new BufferedReader(new FileReader("c:\\Tomcat\\webapps\\midp\\WEB-INF\\classes\\News.txt")); String line=null; //da lasciare line=inputStream.readLine(); while (line != null) { user=line.substring(0,1); if ((user.compareTo(User))==0) { news=news+"; "+line.substring(2,line.length()); } line=inputStream.readLine(); } inputStream.close(); //da lasciare } catch(FileNotFoundException e) {System.out.println("File News.txt non trovato");} catch(IOException e ) {System.out.println("Lettura del file non riuscita");} return news; } } e il file News.txt e': S,Aereo caduto A,Beauty farm nuova S,Formula uno monza Ho due problemi: - Quando la invoco dal browser mi restituisce la scritta: Hai queste news per te:; Aereo ca praticamente mi tronca la stringa. Se invece uso il metodo cerca_news con un main java classico che stampa su standard output mi restituisce tutta la stringa in maniera corretta; perche'? - Nel file ho la coppia user.password,news Non c'e' un metodo gia' fatto per dire leggi la stringa fino a "." e poi di seguito fino a ";" e poi fino alla fine? |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Oct 2002
Città: Roma
Messaggi: 1502
|
quando ottieni la pagina nel browser vedi direttamenteil codice html della pagine per vedere cosa e stato prodotto dalla servlet.
__________________
Sun Certified Java Programmer EUCIP Core Level Certified European Certification of Informatics Professionals |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: May 2003
Messaggi: 1534
|
ok ora ho sistemato.
Come faccio a far rispondere piu' righe di testo nella stessa servlet? Vorrei che sul client apparisse: Ecco le tue news: news1 news2 news3 Per quanto riguarda la separazione della stringa? |
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Oct 2002
Città: Roma
Messaggi: 1502
|
Intendi come fare per far apparire piu righe nel file html?
se questa è la domanda il modo più semplice è inserire <br> ogni volta che vuoi andare a capo. per separare le parti di una stringa puoi utilizzre il metodo indexof() che prende un carattere o una stringa e ti restituisce il primo indice in cui quel carattere o quella srtinga appare nella stringa che voi interepretare, e poi usare substring per ricavare le sottostringhe.
__________________
Sun Certified Java Programmer EUCIP Core Level Certified European Certification of Informatics Professionals |
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: May 2003
Messaggi: 1534
|
Sto sviluppando una servlet che riceve connessioni da un cellulare, legge da un file di testo se ci sono news e se ce ne sono ne restituisce una per riga.
Nel ContentType scrivo text/plain perche' non ho bisogno di html. Quindi inserire nella stringa che restituiro' con out.println(risp) <br> mi puo' servire? |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Oct 2002
Città: Roma
Messaggi: 1502
|
se è semplice testo non serve. per andare a capo basta richiamare println() senza parametri o inserire il carattere "\n"
__________________
Sun Certified Java Programmer EUCIP Core Level Certified European Certification of Informatics Professionals |
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: May 2003
Messaggi: 1534
|
Infatti, ho rispolto con \n
Grazie |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 10:26.