+ servlets interact

pull/1/head
Ivan Olexyn 5 years ago
parent 5317be0cd9
commit d894d89d6c

@ -0,0 +1,5 @@
Copyright (C) 2020 by IO42630 <ivan@olexyn.com>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

@ -4,8 +4,8 @@
<output url="file://$MODULE_DIR$/mispbridge/WEB-INF/classes" /> <output url="file://$MODULE_DIR$/mispbridge/WEB-INF/classes" />
<output-test url="file://$MODULE_DIR$/mispbridge/WEB-INF/classes" /> <output-test url="file://$MODULE_DIR$/mispbridge/WEB-INF/classes" />
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$/../mispbridge">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/../mispbridge/src" isTestSource="false" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />

@ -4,20 +4,20 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4"> version="2.4">
<display-name>Hello, World Application</display-name> <display-name>BridgeServlet, World Application</display-name>
<description> <description>
This is a simple web application with a source code organization This is a simple web application with a source code organization
based on the recommendations of the Application Developer's Guide. based on the recommendations of the Application Developer's Guide.
</description> </description>
<servlet> <servlet>
<servlet-name>HelloServlet</servlet-name> <servlet-name>BridgeServlet</servlet-name>
<servlet-class>mypackage.Hello</servlet-class> <servlet-class>BridgeServlet</servlet-class>
</servlet> </servlet>
<servlet-mapping> <servlet-mapping>
<servlet-name>HelloServlet</servlet-name> <servlet-name>BridgeServlet</servlet-name>
<url-pattern>/mispclient</url-pattern> <url-pattern>/core</url-pattern>
</servlet-mapping> </servlet-mapping>
</web-app> </web-app>

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

@ -1,6 +1,6 @@
<html> <html>
<head> <head>
<title>Sample "Hello, World" Application</title> <title>Sample "BridgeServlet, World" Application</title>
</head> </head>
<body bgcolor=white> <body bgcolor=white>
@ -10,7 +10,7 @@
<img src="images/tomcat.gif"> <img src="images/tomcat.gif">
</td> </td>
<td> <td>
<h1>Sample "Hello, World" Application</h1> <h1>Sample "BridgeServlet, World" Application</h1>
<p>This is the home page for a sample application used to illustrate the <p>This is the home page for a sample application used to illustrate the
source directory organization of a web application utilizing the principles source directory organization of a web application utilizing the principles
outlined in the Application Developer's Guide. outlined in the Application Developer's Guide.
@ -18,10 +18,10 @@ outlined in the Application Developer's Guide.
</tr> </tr>
</table> </table>
<p>To prove that they work, you can execute either of the following links: <p>To prove that they work, TESTyou can execute either of the following links:
<ul> <ul>
<li>To a <a href="hello.jsp">JSP page</a>. <li>To a <a href="hello.jsp">JSP page</a>.
<li>To a <a href="hello">servlet</a>. <li>To a <a href="core">servlet</a>.
</ul> </ul>
</body> </body>

@ -8,13 +8,21 @@
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
public final class Hello extends HttpServlet { public final class BridgeServlet extends HttpServlet {
public Hello() {
private static final String MISP_CLIENT_URL = "http://localhost:9090/mispclient/core";
private List<String> list = new ArrayList<>();
public BridgeServlet() {
} }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
@ -22,7 +30,7 @@ public final class Hello extends HttpServlet {
PrintWriter writer = response.getWriter(); PrintWriter writer = response.getWriter();
writer.println("<html>"); writer.println("<html>");
writer.println("<head>"); writer.println("<head>");
writer.println("<title>Sample Application Servlet Page</title>"); writer.println("<title>MispBridge</title>");
writer.println("</head>"); writer.println("</head>");
writer.println("<body bgcolor=white>"); writer.println("<body bgcolor=white>");
writer.println("<table border=\"0\">"); writer.println("<table border=\"0\">");
@ -33,11 +41,35 @@ public final class Hello extends HttpServlet {
writer.println("<td>"); writer.println("<td>");
writer.println("<h1>Sample Application Servlet</h1>"); writer.println("<h1>Sample Application Servlet</h1>");
writer.println("This is the output of a servlet that is part of"); writer.println("This is the output of a servlet that is part of");
writer.println("the Hello, World application."); writer.println("the BridgeServlet, Very World application.");
writer.println("</td>"); writer.println("</td>");
writer.println("</tr>"); writer.println("</tr>");
writer.println("REQUEST");
writer.println(request.getRequestURI());
writer.println(request.getRequestURL());
writer.println(request.getMethod());
for(String s : list){
writer.println(s);
}
writer.println("</table>"); writer.println("</table>");
writer.println("</body>"); writer.println("</body>");
writer.println("</html>"); writer.println("</html>");
} }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
list.add(request.getRemoteUser());
}
} }

@ -4,10 +4,11 @@
<output url="file://$MODULE_DIR$/mispclient/WEB-INF/classes" /> <output url="file://$MODULE_DIR$/mispclient/WEB-INF/classes" />
<output-test url="file://$MODULE_DIR$/mispclient/WEB-INF/classes" /> <output-test url="file://$MODULE_DIR$/mispclient/WEB-INF/classes" />
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$/../mispclient">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/../mispclient/src" isTestSource="false" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="org.eclipse.jetty.orbit:javax.servlet:3.0.0.v201112011016" level="project" />
</component> </component>
</module> </module>

@ -4,20 +4,20 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4"> version="2.4">
<display-name>Hello, World Application</display-name> <display-name>BridgeServlet, World Application</display-name>
<description> <description>
This is a simple web application with a source code organization This is a simple web application with a source code organization
based on the recommendations of the Application Developer's Guide. based on the recommendations of the Application Developer's Guide.
</description> </description>
<servlet> <servlet>
<servlet-name>HelloServlet</servlet-name> <servlet-name>ClientServlet</servlet-name>
<servlet-class>Hello</servlet-class> <servlet-class>ClientServlet</servlet-class>
</servlet> </servlet>
<servlet-mapping> <servlet-mapping>
<servlet-name>HelloServlet</servlet-name> <servlet-name>ClientServlet</servlet-name>
<url-pattern>/mispbridge</url-pattern> <url-pattern>/core</url-pattern>
</servlet-mapping> </servlet-mapping>
</web-app> </web-app>

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

@ -1,6 +1,6 @@
<html> <html>
<head> <head>
<title>Sample "Hello, World" Application</title> <title>Sample "BridgeServlet, World" Application</title>
</head> </head>
<body bgcolor=white> <body bgcolor=white>
@ -10,7 +10,7 @@
<img src="images/tomcat.gif"> <img src="images/tomcat.gif">
</td> </td>
<td> <td>
<h1>Sample "Hello, World" Application</h1> <h1>Sample "BridgeServlet, World" Application</h1>
<p>This is the home page for a sample application used to illustrate the <p>This is the home page for a sample application used to illustrate the
source directory organization of a web application utilizing the principles source directory organization of a web application utilizing the principles
outlined in the Application Developer's Guide. outlined in the Application Developer's Guide.
@ -21,7 +21,7 @@ outlined in the Application Developer's Guide.
<p>To prove that they work, you can execute either of the following links: <p>To prove that they work, you can execute either of the following links:
<ul> <ul>
<li>To a <a href="hello.jsp">JSP page</a>. <li>To a <a href="hello.jsp">JSP page</a>.
<li>To a <a href="hello">servlet</a>. <li>To a <a href="core">servlet</a>.
</ul> </ul>
</body> </body>

@ -0,0 +1,153 @@
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public final class ClientServlet extends HttpServlet {
private static final String MISP_BRIDGE_URL = "http://localhost:9090/mispbridge/core";
public ClientServlet() {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<head>");
writer.println("<title>MispClient</title>");
writer.println("</head>");
writer.println("<body bgcolor=white>");
writer.println("<table border=\"0\">");
writer.println("<tr>");
writer.println("<td>");
writer.println("<img src=\"images/tomcat.gif\">");
writer.println("</td>");
writer.println("<td>");
writer.println("<h1>Sample Application Servlet</h1>");
writer.println("This is the output of a servlet that is part of");
writer.println("the Hello, World application.");
writer.println("</td>");
writer.println("</tr>");
writer.println(make2ColumnRow("foo", "bar"));
writer.println(make2ColumnRow("sendGet", sendGet()));
writer.println(make2ColumnRow("sendPost", sendPost()));
writer.println("</table>");
writer.println("</body>");
writer.println("</html>");
}
private String make2ColumnRow(String one, String two){
StringBuilder sb = new StringBuilder();
sb.append("<tr>");
sb.append("<td>");
sb.append(one);
sb.append("</td>");
sb.append("<td>");
sb.append(two);
sb.append("</td>");
sb.append("</tr>");
return sb.toString();
}
private String sendGet() throws IOException {
URL url = new URL(MISP_BRIDGE_URL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// By default it is GET request
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", "USER_AGENT");
int responseCode = con.getResponseCode();
System.out.println("Sending get request : " + url);
System.out.println("Response code : " + responseCode);
// Reading response from input Stream
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String output;
StringBuffer response = new StringBuffer();
while ((output = in.readLine()) != null) {
response.append(output);
}
in.close();
return response.toString();
//printing result from response
}
private String sendPost() throws IOException{
URL url = new URL(MISP_BRIDGE_URL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// Setting basic post request
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "USER_AGENT");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type","application/json");
String postJsonData = "{\"id\":5,\"countryName\":\"USA\",\"population\":8000}";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postJsonData);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("nSending 'POST' request to URL : " + url);
System.out.println("Post Data : " + postJsonData);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String output;
StringBuffer response = new StringBuffer();
while ((output = in.readLine()) != null) {
response.append(output);
}
in.close();
//printing result from response
System.out.println();
return response.toString();
}
}
Loading…
Cancel
Save