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
| import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletSocket2 extends HttpServlet{
// variables d'instance
private final String HTML1=
"<html>" +
"<head>" +
"<title>Génération de formulaire</title>"+
"</head>" +
"<body>" +
"<h3>Informations de connexion</h3>"+
"<hr>" +
"<form method=\"POST\">";
private final String HTML2="<input type=\"submit\" value=\"Envoyer\"></form><hr>\n";
private final String HTML3="</body>\n</html>";
// GET
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException{
// on récupère l'éventuel choix de l'utilisateur
String adresse=request.getParameter("adresse");
String port=request.getParameter("port");
// on indique au client le type de document envoyé
response.setContentType("text/html");
// on envoie le formulaire
PrintWriter httpOut=response.getWriter();
httpOut.println(HTML1);
httpOut.println("adresse : <INPUT type=text size=20 name=adresse><br>");
httpOut.println("port : <INPUT type=text size=20 name=port><BR>");
httpOut.println(HTML2);
if ((adresse != "") && (port != "")){
try {
Socket sk = new Socket(adresse,Integer.valueOf(port).intValue());
httpOut.println("Socket ouverte <br>");
BufferedReader strin = new BufferedReader(new InputStreamReader(sk.getInputStream()));
PrintStream strout = new PrintStream(sk.getOutputStream());
strout.println("XCYZ4020047$ujava mon_dam DSOCNX1234567891234567ujava");
httpOut.println("--> XCYZ4020047$ujava mon_dam DSOCNX1234567891234567ujava<br>");
int c;
int a;
httpOut.println("<font color=#009933><--");
for (a=0; a < 67; a++){
c = strin.read();
httpOut.println((char)c);
}
httpOut.println("</font><br>");
strin.close();
strout.close();
sk.close();
httpOut.println("Socket fermée<br>");
} catch (UnknownHostException e) {
httpOut.println("hote inconnu : "+e.getMessage());
} catch (IOException e) {
httpOut.println("erreur entree/sortie : "+e.getMessage());
}
}
// fin du formulaire
httpOut.println(HTML3);
}//GET
// POST
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException{
// on renvoie sur GET
doGet(request,response);
}//POST
}//classe |
Partager