In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what the knowledge about the Swing client is, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
1. Brief introduction
Jakarta Struts is a framework for creating servlet applications based on the MVC pattern. Most Struts applications use browsers as clients, but Struts is actually open enough to use other client modes. Here I will change a browser application example in my "Coding your second Jakarta Struts Application" article to Swing client mode, with only a little code modification.
This article focuses on how to use Swing clients to connect to existing Servlet applications. If you plan to develop a java application that can use either a browser or Swing as a client, you need to choose a framework flexibly according to the needs of your program, such as EJB or web service, which provide a simple interface.
2.Struts structure
Before we begin, let's take a look at how browsers communicate with Struts applications. This will be reflected in our Swing application.
The ◆ Struts application starts when the servlet receives a GET or POST request.
◆ servlet decides which "action" to call and gets the information from URL.
◆ implements that the java class of Action is configured in struts-config.xml.
The output of the ◆ Struts is displayed by jsp, and the name of the jsp is also specified in the struts-config.xml file.
So * non-browser applications need to be able to send a request to servlet and get a response from the jsp page.
3.The URLConnection class
Using the URLConnection class, you can easily write servlet request code using java. This class is interesting because it has some strange designs. If you want to see its implementation, I suggest you read Dodge the traps hiding in the URLConnection class.
If you want to send a request with a "list" action and accept a printed response, you need to write the code like this:
URL url = new URL ("http://myserver/project/list.do"); URLConnection conn = url.openConnection (); BufferedReader in = new BufferedReader (new InputStreamReader (conn.getInputStream (); String line; while ((line = in.readLine ())! = null) {System.out.println (line);}
Generally speaking, you usually need to send some data at the same time as the request-just like submitting a form. This data needs to be sent before you read the response.
URL url = new URL ("http://myserver/project/list.do"); URLConnection conn = url.openConnection (); conn.setDoOutput (true); conn.setRequestProperty (" user-agent "," SWING "); BufferedWriter out = new BufferedWriter (new OutputStreamWriter (conn.getOutputStream ()); out.write (" name1=value1&name2=value2 "); out.flush (); out.close () String c = conn.getHeaderField ("Set-Cookie"); BufferedReader in = new BufferedReader (new InputStreamReader (conn.getInputStream (); String line; while ((line = in.readLine ())! = null) {System.out.println (line);}
This example also shows how to read and write HTTP header information.
The * examples use the GET method to communicate with HTTP, while the second example uses the POST method to communicate. You can also use the GET method to send data to servlet, but you have to add the data to the URL, like this:
URL url = new URL ("http://myserver/project/list.do?name1=value1&name2=value2");
4.Identifying the client
Now we know how to send and request data, but how do we accept it? If the response is for the browser, the response includes either HTML or JavaScript and style. For our Swing client, all we need is data. This raises a new question: how can Struts applications recognize that the client is a browser, a Swing, or something?
One solution is to identify which client it is when parameters are added to each request. I have used the user-agent header information in the second example to identify it as a Swing client.
When the Struts application is ready to return data to the client, it detects the value of the user-agent and selects the jsp page that is appropriate for the client. This will override the jump information set by the Struts Action class.
String client = (String) request.getHeader ("user-agent"); / / Forward control to the list page if (client.equals ("SWING")) return (mapping.findForward ("swinglist")); else return (mapping.findForward ("list")); this is the end of the knowledge about Swing client. I hope the above content can help you to learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.