printwriter

RequestDispatcher

无人久伴 提交于 2019-12-04 05:51:02
一、RequestDispatcher RequestDispatcher实例对象是由Servlet引擎创建的,它用于包装一个要被其他资源调用的资源,例如Servlet、HTML文件,JSP文件等,并可以通过其中的方法将客户端的请求转发给所包装的资源。RequestDispatcher接口定义了forward和include方法,forward用于将请求转发到RequestDispatcher对象封装的资源,include用于将RequestDispatcher对象封装的资源作为当前响应内容的一部门包含进来。 ServletContext接口中定义了两个用于获取RequestDispatcher对象的方法: getRequestDispatcher方法:返回包装了某个路径所指定的资源的RequestDispatcher对象,传递给该方法的路径字符串必须以“/”开头,“/”代表当前Web应用程序的根目录(虚拟目录)。WEB-INF目录中的内容对RequestDispatcher对象是可见的,所以,传递给getRequestDispatcher方法的资源可以是WEB-INF目录中不能被外界访问的文件。 getNamedDispatcher方法:返回包装了某个Servlet或JSP文件的RequestDispatcher对象

I've created a Java server with sockets, just how do print to ALL sockets?

大城市里の小女人 提交于 2019-12-04 03:43:49
I've been trying this for a while, and I want multiple clients to recieve multiple inputs simultaneously. There is one problem, I want the server to print "Hi" to all clients if one client says 'print2all Hi'. I know how to process it to print it, just to print to ALL clients is the problem. Here's what I have so far. Server try{ try{ server = new ServerSocket(25565); } catch (Exception e){ e.printStackTrace(); } while (isListening){ new SocketThread(server.accept()).start(); } server.close(); } catch (Exception e){ e.printStackTrace(); } SocketThread try { PrintWriter out = new PrintWriter

servlet学习(三)--HttpServletResponse

陌路散爱 提交于 2019-12-03 16:52:14
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象。 request和response对象即然代表请求和响应,那我们要获取客户机提交过来的数据,只需要找request对象就行了。要向客户机输出数据,只需要找response对象就行了。 一、HttpServletResponse对象介绍      HttpServletResponse对象代表服务器的响应。这个对象中封装了向客户端发送数据、发送响应头,发送响应状态码的方法。查看HttpServletResponse的API,可以看到这些相关的方法。 1.1、负责向客户端(浏览器)发送数据的相关方法    1.2、负责向客户端(浏览器)发送响应头的相关方法       1.3、负责向客户端(浏览器)发送响应状态码的相关方法    1.4、响应状态码的常量   HttpServletResponse定义了很多状态码的常量(具体可以查看Servlet的API),当需要向客户端发送响应状态码时,可以使用这些常量,避免了直接写数字,常见的状态码对应的常量:   状态码404对应的常量      状态码200对应的常量      状态码500对应的常量    二、HttpServletResponse对象常见应用 2.1

I created a PrintWriter with autoflush on; why isn't it autoflushing?

霸气de小男生 提交于 2019-12-03 12:59:39
My client is a web browser, and sending request to myserver using this url: http://localhost This is the server side code. The problem lies in the run method of the ServingThread class. class ServingThread implements Runnable{ private Socket socket ; public ServingThread(Socket socket){ this.socket = socket ; System.out.println("Receives a new browser request from " + socket + "\n\n"); } public void run() { PrintWriter out = null ; try { String str = "" ; out = new PrintWriter( socket.getOutputStream() ) ; out.write("This a web-page.") ; // :-( out.flush() ; // :-( socket.close() ; System.out

kCFStreamPropertySocketSecurityLevel to kCFStreamSocketSecurityLevelNegotiatedSSL causes OSStatus errSSLXCertChainInvalid (-9807) connecting to Java

匿名 (未验证) 提交于 2019-12-03 09:14:57
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a simple Java server that uses a self-signed cert to identify itself that I created with keytool: System.setProperty("javax.net.ssl.keyStore", "../../pki/z-keystore.jks"); System.setProperty("javax.net.ssl.keyStorePassword", "ZZZZZZ"); System.setProperty("javax.net.debug", "all"); ServerSocketFactory serverSocketFactory = SSLServerSocketFactory .getDefault(); ServerSocket serverSocket = serverSocketFactory .createServerSocket(8443); System.out.println("Waiting for connections on 8443"); final AtomicInteger nextSocketId = new

How to make the PrintWriter to write UTF-8?

♀尐吖头ヾ 提交于 2019-12-03 08:08:33
问题 How to make the PrintWriter to write UTF-8? pstream = new PrintWriter(csocket.getOutputStream(), true); String res = "some string"; pstream.println(res); // here I want to output string as UTF-8 回答1: Use an OutputStreamWriter : pstream = new PrintWriter(new OutputStreamWriter( csocket.getOutputStream(), StandardCharsets.UTF_8), true) 回答2: Look at Java: Difference between PrintStream and PrintWriter discussion. To be quick: you can use -Dfile.encoding=utf8 JVM parameter or method suggested in

序列化反序列化流、打印流

回眸只為那壹抹淺笑 提交于 2019-12-03 06:39:42
ObjectOutputStream(序列化流)   ObjectOutputStream是序列化流,可以将Java程序中的对象写到文件中。   ObjectOutputStream 构造方法:     ObjectOutputStream(OutputStream out):参数要传递字节输出流。   ObjectOutputStream写对象的方法(特有方法):     void writeObject(Object obj): 向文件中写对象。   ObjectOutputStream 的使用步骤:     创建序列化流,用来写。     调用 writeObject 方法,写对象。     释放资源。   注意: 要使用序列化流向文件中写的对象,必须实现 Serializable 接口。    例: 1 // 创建一个Person对象 2 Person p=new Person("大灰狼",18); 3 //明确目的地 4 FileOutputStream fos=new FileOutputStream("D:\\demo0723\\person.txt");//不能续写true 5 //创建序列化流 6 ObjectOutputStream oos=new ObjectOutputStream(fos); 7 //将对象写入文件中 8 oos.writeObject(p

Why does resuming an activity in android cause BadTokenException?

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Folks - Can anyone explain this stack? Note that my code is nowhere on it. If you Google for any of these exceptions, everyone who has experiencing this issue was trying to create dialogs after an activity was terminated, which doesn't seem to be the case here. It's just a simple activity resume. I am seeing this exception reported from clients in the field quite frequently and would like to correct it if possible. android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@405177d8 is not valid; is

setDefaultUncaughtExceptionHandler makes app crash silently

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: CustomExceptionHandler public class CustomExceptionHandler implements UncaughtExceptionHandler { private Context ctx; private ContentResolver cr; public CustomExceptionHandler(Context ctx, ContentResolver cr) { this.ctx = ctx; this.cr = cr; } public void uncaughtException(Thread t, Throwable e) { final Writer result = new StringWriter(); final PrintWriter printWriter = new PrintWriter(result); e.printStackTrace(printWriter); String stacktrace = result.toString(); printWriter.close(); String deviceUuid = Utilities.DeviceUuid(ctx, cr); String

Scala: write string to file in one statement

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: For reading files in Scala, there is Source.fromFile("file.txt").mkString Is there an equivalent and concise way to write a string to file? Most languages support something like that. My favorite is Groovy: def f = new File("file.txt") // Read def s = f.text // Write f.text = "file contents" I'd like to use the code for programs ranging from a single line to a short page of code. Having to use your own library doesn't make sense here. I expect a modern language to let me write something to a file conveniently. There are posts similar to this