A Java Web dynamic server is written mainly through internal classes. Dynamic classes use external classes and
Classforname is instantiated. The dynamic class constructor cannot contain parameters, and the results are all displayed. share the results with the required
Friend. There are not enough judgments,
I am grateful for pointing out the shortcomings. The following code is used:
Static file index.html:
<HTML>
Core code:
Import Java. io. bufferedreader; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstream; import Java. io. printwriter; import Java. lang. reflect. invocationtargetexception; import Java. lang. reflect. method; import java.net. malformedurlexception; import java.net. serversocket; import java.net. socket; import java.net. URL; import java.net. urlclassloader;/*** web processing class */class webprocess {private request; private response; Public webprocess (request, response) {// todo auto-generated constructor Stub this. request = request; this. response = response; this. process ();} private void process () {// todo auto-generated method stub string uri = request. geturi (); system. out. println ("URI -------------------------" + URI); string urisuffix = Uri. substring (URI. indexof ('. ') + 1); system. out. println ("urisuffix ------------" + urisuffix); If ("htmlxmlhtm ". indexof (urisuffix)>-1) {system. out. println ("static file"); file filetosend = new file (URI); If (filetosend. exists () {system. out. println ("file"); try {fileinputstream FD = new fileinputstream (filetosend); byte [] DATA = new byte [FCM. available ()]; FCM. read (data); // system. out. println (data. length); response. out (new string (data);} catch (filenotfoundexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}} else {system. out. println ("file does not exist"); response. out ("the webpage you want to access does not exist") ;}} else {system. out. println ("dynamic file"); // visit v = new visit (response, request); file filetosend1 = new file (URI); system. out. println ("filetosend --------------" + filetosend1);/* If (filetosend1.exists () {* // system. out. println ("class file exists"); try {// obtain the required class path url = new URL ("file: \ D: \ Users \ Administrator \ workspaces \ myeclipse 8.6 \ 2013-08-21 \ SRC "); string classname = URI; system. out. println ("url ---------------" + URL); classloader Cl = new urlclassloader (new URL [] {URL}); Class D = Cl. loadclass (classname); system. out. println (d); // system. out. println ("Cl -----------------------" + Cl); Object C = Class. forname (URI ). newinstance (); Method method = C. getclass (). getmethod ("process", response. class, request. class); method. invoke (C, response, request);} catch (malformedurlexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (classnotfoundexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (instantiationexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (illegalaccessexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (securityexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (nosuchmethodexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (illegalargumentexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (invocationtargetexception e) {// todo auto-generated Catch Block E. printstacktrace ();}/*} else {system. out. println ("class file does not exist");} */}/*** response class */class response {private printwriter writer; public response (outputstream) {// todo auto-generated constructor Stub this. writer = new printwriter (outputstream, true); // This. out (data);} public void out (string data) {// todo auto-generated method stub system. out. println ("Out () --------------- start"); writer. println (data) ;}}/*** request class */class request {private inputstream input; public request (inputstream) {// todo auto-generated constructor Stub this. input = inputstream; // This. geturi ();} Public String geturi () {string resource = NULL; system. out. println ("geturi () ----------------- start --"); bufferedreader in = new bufferedreader (New inputstreamreader (input); try {string line = in. readline (); resource = line. substring (line. indexof ('/') + 1, line. lastindexof ('/')-5); system. out. println ("resource -----------" + Resource);} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();} return resource;}/*** @ author administrator * uses the thread */public class serverweb implements runnable {Private Static final int Port = 8085; serversocket; /*** @ Param ARGs */Public serverweb () {New thread (this ). start (); // set the current class to a thread} @ override public void run () {// todo auto-generated method stub system. out. println ("Run () ------------------- start -----"); try {serversocket = new serversocket (port); system. out. println ("serversocket ------------------" + serversocket. getlocalport (); While (true) {Socket socket = serversocket. accept (); system. out. println ("socket --------------------" + socket. getlocalport (); Request request = new request (socket. getinputstream (); Response response = new response (socket. getoutputstream (); webprocess = new webprocess (request, response); socket. close () ;}} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}} public static void main (string [] ARGs) {// todo auto-generated method stub new serverweb ();}}
Code for independent dynamic classes:
Import Java. util. date; public class visit {public visit (/* response, request */) {// todo auto-generated constructor stub/* This. request = request; this. response = response; * // This. process ();} public void process (Response response, request) {response. out ("dynamic web page" + new date (). tostring (); system. out. println ("process ------------ start ");}}