Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8720479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:03:00+00:00 2026-06-13T07:03:00+00:00

I want to develop an application which has communication between a servlet file running

  • 0

I want to develop an application which has communication between a servlet file running on Tomcat.

Below is the code in my application, which is trying to connection to this servlet, send request and get response.

 private URLConnection getServletConnection() {
    try {
        URL servletURL = new URL("http://localhost:8080/test/servlet");
        URLConnection conn = servletURL.openConnection();

        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        return conn;
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

And below is the code where exception is thrown:

URLConnection conn = getServletConnection();
OutputStream outputStream = conn.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outputStream);
ServletRequestMessage srm = new ServletRequestMessage(2, username, password);
oos.writeObject(srm);
oos.flush();
oos.close();
InputStream inputStream = conn.getInputStream();

The exception is:

java.io.FileNotFoundException: http://localhost:8080/test/servlet

Can anyone help me? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:03:03+00:00Added an answer on June 13, 2026 at 7:03 am

    Server Side

    public class ServletImpl extends HttpServlet implements Servlet {
    
        ....
        public ServletImpl() {
          super();      
        }
    
     public void init(ServletConfig config) throws ServletException{
        super.init(config);         
    
    
        /*
         Application scope 
         Shared between all servlets, JSP pages, and custom tags within a J2EE application 
         or within the whole container if no applications are defined.
         The programmatic interface to the application scope is the 'ServletContext' object.         
         */
    
    
        ServletContext context = config.getServletContext();
        context.setAttribute("base", config.getInitParameter("base"));
          /* where "base" is iniy param in web.xml
             <init-param>
        <param-name>base</param-name>
        <param-value>/ServlrtName/sys</param-value>
        </init-param>       
    
    
           */
    
    
    
    ....
    
    
    
    }
        ....
    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
    {
        doPost(request, response);
    }
    
    
        protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
       {
        Enumeration<?> paramNames = request.getParameterNames();
    
    
        while(paramNames.hasMoreElements()) {
    
            String paramName = (String)paramNames.nextElement();
    
            String[] paramValues = request.getParameterValues(paramName);
    
    
             if("sub".equals(paramName)){
    
                paramValues = request.getParameterValues(paramName);
    
                if(paramValues.length > 0){
    
                    String param = paramValues[0];
                    // do something
                    ....
                }               
    
            }           
        }
    }
    
    
    ....
    // prepear response 
        response.setContentType("text/html");
    
        PrintWriter out = response.getWriter();
    
        out.println(mMessageResponseStr);
        out.close();
    

    Here I used sub tag, see: if("sub".equals(paramName)){.

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
     <display-name>ServlrtName</display-name>
     <servlet>
        <description>
        </description>
        <display-name>ServlrtName</display-name>
        <servlet-name>ServlrtName</servlet-name>
        <servlet-class>com.demo.servlet.ServletImpl</servlet-class>
      <init-param>
        <param-name>base</param-name>
        <param-value>/ServlrtName/sys</param-value>
        </init-param>
      <init-param>
    
            ....
    
    </servlet>
    
    ...
    
     <servlet-mapping>
        <servlet-name>ServlrtName</servlet-name>
        <url-pattern>/sys/*</url-pattern>
      </servlet-mapping>
    

    Client Side

    I used DefaultHttpClient and HttpPost. I send sub tag . Here is a method that sends data to Servlet:

     public boolean send(String data) {
    
        DefaultHttpClient httpclient = null;
        boolean success = false;
    
        try {           
            httpclient = new DefaultHttpClient();
    
            String url = "your URL";
    
    
            HttpPost httpost = new HttpPost(url);
    
            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("sub", data));
    
    
            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    
    
            HttpResponse response = httpclient.execute(httpost);
    
            HttpEntity entity = response.getEntity();
    
            if (entity != null) {
    
                StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();
    
                if(statusCode != 200){
                    mResErr.onErrorResponse(statusCode);                    
                }
    
                InputStream is = entity.getContent();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                while ( (line = br.readLine()) != null) {
    
                    // get data from line
    
    
                }
                is.close();
    
    
            } else {
                //response is null/
            }
            success = true;
    
            mRes.onHttpResponse(mArr);
    
    
        } catch (Exception e) {
            mResErr.onErrorResponse(e);
            e.getStackTrace();
        }
    
        if (httpclient != null) {
            // resource cleanup
            httpclient.getConnectionManager().shutdown();
        }
    
        return success;
    }   
    

    ** Comment, before you start check connectivity, remove user/password from server side. If all works as expected switch it back and use on client side:

    Credentials cred = new UsernamePasswordCredentials("user", "pswd");
    
    
             httpclient.getCredentialsProvider().setCredentials(
                        new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                        cred); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to develop an iPhone application which has to record the phone conversation.
I am newbie in android .i want to develop an application which has the
I'm trying to develop an application in which I want the ability for certain
I want to develop an application in which I need to know the position
I want to develop an application in which firstly i was develop an structure
i want to develop the blog application which is described in the book ASP.NET
I am new to Java Swing I want to develop an POS application which
I develop an application which has several tasks like report generator, show history etc,
I'm trying to develop a new application based on the standard Utility template, which
I am new to programming. I am trying to develop an application in which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.