Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Servlet writing and reading String from Socket

914465
Member Posts: 2
Hello,
I have a webpage with one link to one Servlet.
this Servlet is supposed to connect to one (working) server on port 4445.
I send one string to the server and he invokes one method and respondes with another String.
The servlet correctly sends the string to the server the method on the server is working but the Servlet is not getting the response from the server. The server method for sending is well implemented.
The servlet is supposed to print HTML code after receiving something. I'm using TOMCAT7 as the servlet container.
Can you please help?
thank you very much!
THREAD ON THE SERVER:
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String input = in.readLine();
if (input.equals("listar"))
{
System.out.println("RECEIVED"+input);
out.print(ServidorSubs.listaEcrasValues());
System.out.println("SENT"+ServidorSubs.listaEcrasV alues());
}
else
System.out.println("RECEIVED"+input);
in.close();
out.close();
socket.close();
}catch (IOException e){
e.printStackTrace();
}
}
SERVLET CODE:
/**
* envia para o servidor o pedido de listar
* o que este tem no HashMap de ecrãs
*/
import java.net.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ListarEcras extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// IP do servidor
String servidor = "127.0.0.1";
listar(servidor,4444);
}
public void listar (String servidor, int porta)
{
String ecras = null;
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
try { //1.Open a socket.
socket = new Socket(servidor, porta);
//Open an output stream to the socket.
out = new PrintWriter(socket.getOutputStream(), true);
//Open an intput stream to the socket.
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " +servidor);
System.exit(1);
} catch (IOException e) {
System.err.println("Não consigo criar o OutputStream para o :" +servidor);
System.exit(1);
}
//write to the stream
out.println("listar");
//WAIT FOR RESPONSE FROM SERVER
try{
ecras = in.readLine();
out.println("<html>");
out.println("<head>");
out.println("<title> " + ecras + "</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}catch (IOException e){
System.err.println("Erro: "+e.getMessage());
System.exit(1);
}
try{
//close stream
out.close();
in.close();
//close o socket
socket.close();
}catch (IOException e) {
System.err.println("Não consegui fechar os sockets");
System.exit(1);
}
}
}
I have a webpage with one link to one Servlet.
this Servlet is supposed to connect to one (working) server on port 4445.
I send one string to the server and he invokes one method and respondes with another String.
The servlet correctly sends the string to the server the method on the server is working but the Servlet is not getting the response from the server. The server method for sending is well implemented.
The servlet is supposed to print HTML code after receiving something. I'm using TOMCAT7 as the servlet container.
Can you please help?
thank you very much!
THREAD ON THE SERVER:
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String input = in.readLine();
if (input.equals("listar"))
{
System.out.println("RECEIVED"+input);
out.print(ServidorSubs.listaEcrasValues());
System.out.println("SENT"+ServidorSubs.listaEcrasV alues());
}
else
System.out.println("RECEIVED"+input);
in.close();
out.close();
socket.close();
}catch (IOException e){
e.printStackTrace();
}
}
SERVLET CODE:
/**
* envia para o servidor o pedido de listar
* o que este tem no HashMap de ecrãs
*/
import java.net.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ListarEcras extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// IP do servidor
String servidor = "127.0.0.1";
listar(servidor,4444);
}
public void listar (String servidor, int porta)
{
String ecras = null;
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
try { //1.Open a socket.
socket = new Socket(servidor, porta);
//Open an output stream to the socket.
out = new PrintWriter(socket.getOutputStream(), true);
//Open an intput stream to the socket.
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " +servidor);
System.exit(1);
} catch (IOException e) {
System.err.println("Não consigo criar o OutputStream para o :" +servidor);
System.exit(1);
}
//write to the stream
out.println("listar");
//WAIT FOR RESPONSE FROM SERVER
try{
ecras = in.readLine();
out.println("<html>");
out.println("<head>");
out.println("<title> " + ecras + "</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}catch (IOException e){
System.err.println("Erro: "+e.getMessage());
System.exit(1);
}
try{
//close stream
out.close();
in.close();
//close o socket
socket.close();
}catch (IOException e) {
System.err.println("Não consegui fechar os sockets");
System.exit(1);
}
}
}
Answers
-
out.print(ServidorSubs.listaEcrasValues());Change that to println(). You are reading lines but you aren't sending them.
-
Thank you very very much.
Still cant understand why it didnt work because the method ServidorSubs.listaEcrasValues() returns one string....and I was printing it to the Socket Stream with the PrintWriter...
I replaced that with out.println("Info: "+ServidorSubs.listaEcrasValues()); and it was sent to the socket and received by the servlet.
I want to be able to send to the servlet several strings using a while loop and then print it on the browser.
once again thank you very much. -
Strange, I told you.You are reading lines but you aren't sending themStill cant understand why it didnt work
This discussion has been closed.