13559

how to implement client and server code in jsp

Question:

I have a small chatting App in Java SE for Desktop, I want to implement the same for Web as well, I have ChatServer as :

public class ChatServer extends Thread { public static final int DEFAULT_PORT = 6035; public static String initServer() { String msg = ""; int port = DEFAULT_PORT; ServerSocket serverSocket = null; Socket socket = null; try { serverSocket = new ServerSocket(port); msg = "Listening on port " + port; while (true) { socket = serverSocket.accept(); /*System.out.println("Connection receive from " + socket.getRemoteSocketAddress());*/ ChatHandler handler = new ChatHandler(socket); handler.start(); } } catch (IOException ioe) { System.err.println("Usage: java ChatServer [port]"); System.err.println("Where options include:"); System.err.println("\tport the port on which to listen."); msg = "Usage: java ChatServer [port]"; System.exit(0); } finally { try { serverSocket.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } return msg; } }

Now to call this in index.jsp, I have :

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body>

<%=pkj.com.lenvel.ChatServer.initServer() %>

</body> </html>

the above code is not showing any message and I'm still confused, where to implement code for Client?

Answer1:

Try this

Server (YourServer.java)

import java.io.*; import java.net.*; public class YourServer { public static void main(String[] args ) { try { ServerSocket socket = new ServerSocket(8765); Socket insocket = socket.accept( ); BufferedReader in = new BufferedReader (new InputStreamReader(insocket.getInputStream())); PrintWriter out = new PrintWriter (insocket.getOutputStream(), true); String instring = in.readLine(); out.println("The server got this: " + instring); insocket.close(); } catch (Exception e) {} } }

<strong>Client.Jsp</strong>

<%@ page import="java.io.*, java.net.*" %> <HTML> <HEAD> <TITLE>Creating Client/Server Applications</TITLE> </HEAD> <BODY> <H1>Creating Client/Server Applications</H1> <% try{ int character; Socket socket = new Socket("127.0.0.1", 8765); InputStream inSocket = socket.getInputStream(); OutputStream outSocket = socket.getOutputStream(); String str = "Hello!\n"; byte buffer[] = str.getBytes(); outSocket.write(buffer); while ((character = inSocket.read()) != -1) { out.print((char) character); } socket.close(); } catch(java.net.ConnectException e){ %> You must first start the server application (YourServer.java) at the command prompt. <% } %> </BODY> </HTML>

<a href="http://www.java2s.com/Tutorial/Java/0360__JSP/CreatingClientServerApplications.htm" rel="nofollow">Taken from this link</a>

Recommend

  • Error when trying to send an email using Gmail API in Java
  • How to restrict number of concurrent processes?
  • Simple command-line app I/O in Dart
  • Can't configure nginx as a proxy for tomcat with context path
  • Selenium and Google - How do you use cookies?
  • 302 Redirect from http to https in Android using Dropbox short Hyperlinks
  • What is wrong in my MVC implementation?
  • Apache Lucene - Improving the results of Spell Checker
  • Extracting a process's exit code in the case of ThreadInterrupted
  • problem with classes not found during PlayPlugin.enhance
  • Runtime error in UVA Online Judge [closed]
  • Use neo4j server instead of embedded mode
  • 3.0.0.M1: SSL - Invalid keystore format
  • How to handle empty space in url when downloading image from web?
  • Unix Network Programming Clarification
  • Problem with rejecting incoming call
  • How to copy styled text in JTextPane
  • Ubuntu and bcrypt
  • How to remove a SwiftyJSON element?
  • Using JRuby with Rails 3.2
  • Code in Job's Script Block after Start-Process Does not Execute
  • How to test if a URL from an Eclipse bundle is a directory?
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Optimizing database types to compact database (SQLite)
  • Volley JsonObjectRequest send headers in GET Request
  • TFS: Get latest causes slow project reloading
  • Obtain ObjectIdHex value from mgo query
  • Running a C# exe file
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Proper way to use connect-multiparty with express.js?
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • How can I use `wmic` in a Windows PE script?
  • Conditional In-Line CSS for IE and Others?
  • Why do underscore prefixed variables exist?
  • How to push additional view controllers onto NavigationController but keep the TabBar?