communication servlet vers serveur
Bonjour,
J'ai implémenté do code de cette servlet qui doit communiquer entre une application cliente et un serveur dont voici le code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Application extends HttpServlet {
private static final long serialVersionUID = 644942366480920087L;
private static String[] tab = null;
private static String url = "url";
private static boolean identBon = true;
public Application(){
}
public void destroy(){
super.destroy();
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
PrintWriter out = res.getWriter();
while(url != null){
for(int i = 0; i <= tab.length; i++){
try{
url = URLDecoder.decode(tab[i].toString(), "UTF-8");
}
catch(UnsupportedEncodingException e){
out.println(e);
}
}
if(url.substring(0).equals("UniqueID")){
if("1015".equals("login") || "1015".equals("password") || "0000".equals("password")){
out.println("C'est un Login et password valides" + identBon);
out.println("login :"+ req.getParameter("login"));
out.println("pass word :"+ req.getParameter("password"));
out.println("UniqueID :"+ req.getParameter("UniqueID"));
try{
InputStream is = new FileInputStream("http://localhost:8080/converso-server/"); //505-1.wwq converso t'envoie le fichier demandé par mobile
OutputStream os = res.getOutputStream();
res.setContentType("text/plain");
res.setHeader("Content-Disposition", "attachment;filename = testNumeric.wwq");
int count;
byte buf[] = new byte[4096];
while((count = is.read(buf)) > -1){
os.write(buf, 0, count);
is.close();
os.close();
}
}
catch(IOException ex){
ex.getMessage();
out.println("Le chargement du fichier a échoué!!!");
}
out.println("Le fichier était bien chargé, Ok!!");
out.flush();
out.close();
}
else{
out.println("Mauvaise identification."+ !identBon +"Réessayer encore");
}
}
else{
out.println("Rien ne correspond à UniqueID");
}
}
}
public void init() throws ServletException{
super.init();
}
} |
Quand j'essaie de débogguer la JVM m'affiche cette erreur:
Citation:
11 juil. 2008 11:06:41 org.apache.catalina.core.ApplicationContext log
INFO: org.apache.webapp.balancer.BalancerFilter: init(): ruleChain: [org.apache.webapp.balancer.RuleChain: [org.apache.webapp.balancer.rules.URLStringMatchRule: Target string: News / Redirect URL:
http://www.cnn.com], [org.apache.webapp.balancer.rules.RequestParameterRule: Target param name: paramName / Target param value: paramValue / Redirect URL:
http://www.yahoo.com], [org.apache.webapp.balancer.rules.AcceptEverythingRule: Redirect URL:
http://jakarta.apache.org]]
Merci, pour une éventuelle aide!
communication entre servlet et serveur
Bnjour,
Ma servlet doit assurer la communication entre une application mobile et le serveur en lecture et écriture de façon bidirectionnelle.
Voici l'exemple:
public class Application extends HttpServlet {
private static final long serialVersionUID = 644942366480920087L;
public Application(){
assert(characterEncoding != null);
}
public void destroy(){
super.destroy();
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
String AN = req.getParameter("AN");
System.out.println("Nom application"+ AN);
String Id = req.getParameter("Id");
System.out.println("Identifiant application"+ Id);
URL url = new URL("http://localhost:8080/server/");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestMethod("POST");
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Content-Type", "application/x-www-x-from-urlencoded");
urlc.connect();
DataInputStream is = new DataInputStream(urlc.getInputStream());
if(urlc != null && decoder != null){
try{
if(decoder.substring(0).equals("UniqueID")){
if("1015".equals("login") || "1015".equals("password") || "0000".equals("password")){
out.println("Valider : login" + "password");
int count = 0;
byte buf[] = new byte[255];
OutputStream os = urlc.getOutputStream();
while((count = is.read(buf)) > 0){
os.write(buf, 0, count);
}
out.println(buf);
is.close();
os.close();
}
}
}
catch(MalformedURLException e){
e.getStackTrace();
}
catch(IOException ex){
ex.getMessage();
}
out.flush();
out.close();
}
public void init() throws ServletException{
super.init();
}
}