Sie sind auf Seite 1von 11

SodhanaLibrary

HOME

JQUERY

ANGULAR

CSS3

JAVA

HTML5

SASS

PLUGINS

ALGORITHMS

AWS

FLAT ICON

Blogroll
Srinivas Dasari
find me on facebook

Proxy Servlet to Forward Requests to remote Server


21:12 | Posted by Dasari Srinivas |
Like

follow me on twitter

Like 164 people like this. Sign Up to see w hat


your friends like.
68

Once I got a project of converting PHP project to JAVA project. I don't have any experience with

Follow this blog by Email

PHP. I tried to run PHP and Java on single server but I couldn't setup well. Then I just got an Idea
of Proxy Servlet and I built one Proxy Servlet to forward requests to another server. In this
article I will explain how I did this.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

Email address...

Subm

Convert XML or JSON to Java Pojo

pdfcrowd.com

Classes
Change Icon Color Online

Popular Posts
PHP project version is running in remote server. Here Proxy Servlet forward the PHP requests to
remote server.

Java RESTFUL web service tutorial wi


sample case study
Introduction: REST stands for

Process

Representational State Transfer and

1. Client (User) sends HTTP requests to Tomacat server


2. Proxy Filter detects *.php requests and forward it to Proxy Servlet
3. Proxy servlet creates GET or POST to Remote server based on HTTP request type
4. Proxy servlet get the response from Remote server and send that response to Client (User)

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

based on the doctoral dissertation of


Roy Fielding . In restful...
Circular Progress with SVG and
JavaScript
Circular Progress Display is pretty

pdfcrowd.com

good style to show the progress for

ProxyFilter.java
This file is responsible for filtering PHP requests from other requests

Image Uploading and for Voting


System. Majority of browsers su...
Add Class, Remove Class or Toggle
Class to div using AngularJS

package proxy;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

AngularJS supports addClass,

removeClass jQuery functionality. He

in below examples we can see how to


add a class to an element. Angul...

Circular Progress With CSS and jQue


Circular Progress Display is pretty
good style to show the progress for
Image Uploading and for Voting
System. In this article you can fi...
jRMenuMore - jQuery plugin for

public class ProxyFilter implements Filter {

Responsive Menu or Navigation Bar


with More option

public ProxyFilter() {

I have written one article on making

Responsive Menu with MORE option a

http://blog.sodhanalibrary.com/2014
public void destroy() {

menu-or-nav...

Responsive Menu or Navigation Bar


With MORE Option With jQuery

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, Servle
open in browser PRO version Are you a developer? Try out the HTML to PDF API
pdfcrowd.com

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, Servle
Some times we have to show Menu o
HttpServletRequest req = (HttpServletRequest)request;
Navigation Bar with dynamic items.
// Store request path to HTTP Request object
Because of various screen resolution
request.setAttribute("uri", req.getRequestURI().substring(req.getContextPath().length()));
like Mobile and PC the Me...
// Forward filtered requests to MyProxy servlet
request.getRequestDispatcher("/ProxyServlet").forward(request, response);
}

Reddit Ranking Algorithm


Implementation with SQL

Rank of post in Reddit is based on U

public void init(FilterConfig fConfig) throws ServletException {


}

Votes and Down-Votes and Age of t

post. As per the Reddit Source Code


on Github , The ran...
Set Attribute or Remove Attribute

using AngularJS
AngularJS supports .attr(),

web.xml entry for ProxyFilter.java

.removeAttr() jQuery functionality.


Here in below examples we can see

This below web.xml configuration is responsible for sending *.php requests to

how to set attribute and remove

ProxyServlet.java

attribut...
Read JSON with JAVA using Google-

<filter>
<display-name>ProxyFilter</display-name>
<filter-name>ProxyFilter</filter-name>
<filter-class>proxy.ProxyFilter</filter-class>

In this article I am going to explain


how to read JSON with JAVA using

Google-json library . The basic part o

</filter>

the JSON is JsonElement. T...

<filter-mapping>

open in browser PRO version

gson library

Are you a developer? Try out the HTML to PDF API

Proxy Servlet to Forward Requests t


pdfcrowd.com

<filter-name>ProxyFilter</filter-name>

Proxy Servlet to Forward Requests t

<url-pattern>*.php</url-pattern>

remote Server

Once I got a project of converting PH

</filter-mapping>

project to JAVA project. I don't have


any experience with PHP. I tried to

ProxyServlet.java

run PHP and Ja...

This creates GET and POST request dynamically to remote server

Catagories
Algorithm
package proxy;

AngularJS

API

CSS

Engine AWS Blog BUTTON CSS


ENGLISH

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

Experiment

FACEBOOK FOLL

GOOGLE html HTML5 Icon Image

JavaScript

jQuery

JAV

Linkedin

PhoneGap Pinterest Plugin Responsive


SASS

Scroll

Search

SQL Startup

Tes

Twitter XML XSS XSTREAM

import java.net.URL;
import java.net.URLEncoder;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

public class ProxyServlet extends HttpServlet {


private static final long serialVersionUID = 1L;
private final String USER_AGENT = "Mozilla/5.0";
public ProxyServlet() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException


//

Create Get request dynamically to remote server

String url = "http://ipaddress:port/contextpath"+request.getAttribute("uri")+"?"+request.getQueryString();

URL obj = new URL(url);


HttpURLConnection con = (HttpURLConnection) obj.openConnection();

// optional default is GET


con.setRequestMethod("GET");

//add request header


con.setRequestProperty("User-Agent", USER_AGENT);

int responseCode = con.getResponseCode();


System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

BufferedReader in = new BufferedReader(


new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response1 = new StringBuffer();

ServletOutputStream sout = response.getOutputStream();

while ((inputLine = in.readLine()) != null) {


response1.append(inputLine);
sout.write(inputLine.getBytes());
}
in.close();

sout.flush();

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException


//

Create Post request dynamically to remote server

String url = "http://ipaddress:port/contextpath"+request.getAttribute("uri");

URL obj = new URL(url);


HttpURLConnection con = (HttpURLConnection) obj.openConnection();

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

//add reuqest header


con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

StringBuilder sb = new StringBuilder();


for(Entry<String, String[]> e : request.getParameterMap().entrySet()){
if(sb.length() > 0){
sb.append('&');
}
String[] temp =e.getValue();
for(String s:temp) {
sb.append(URLEncoder.encode(e.getKey(), "UTF-8")).append('=').append(URLEncoder.encode(s, "UTF-8"));
}
}

String urlParameters = sb.toString();

// Send post request


con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

wr.flush();
wr.close();

int responseCode = con.getResponseCode();


System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(


new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response1 = new StringBuffer();

ServletOutputStream sout = response.getOutputStream();

while ((inputLine = in.readLine()) != null) {


response1.append(inputLine);
sout.write(inputLine.getBytes());
}
in.close();

sout.flush();
}

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

web.xml entry for ProxyServlet.java


<servlet>
<description></description>
<display-name>ProxyServlet</display-name>
<servlet-name>ProxyServlet</servlet-name>
<servlet-class>proxy.ProxyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProxyServlet</servlet-name>
<url-pattern>/ProxyServlet</url-pattern>
</servlet-mapping>

Labels:JAVA
+1 Recommend this on Google

from Srinivas Dasari's production

0 comments:
Post a Comment
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Enter your comment...

Comment as:

Publish

Select profile...

Preview

Newer Post

Home

Older Post

Subscribe to: Post Comments (Atom)

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Das könnte Ihnen auch gefallen