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 8853905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:45:39+00:00 2026-06-14T13:45:39+00:00

I am trying to send some data from Android Client to HttpServlet. My Servlet

  • 0

I am trying to send some data from Android Client to HttpServlet. My Servlet code is as follows

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class DBServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection connection= null;   

/**
 * @see HttpServlet#HttpServlet()
 */
public DBServlet() {
        super();
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {

    doPost(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    if(request.getParameterNames().hasMoreElements()){
                insertRecord(request, getDbConnection());
            }
            else{ System.out.println("Servlet Started First Time"); }



}

/**
 * Return database connection
 * @return
 */
private Connection getDbConnection(){
        String connectionURL = "jdbc:mysql://127.0.0.1:3306/testDB";

        try {

            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(connectionURL, "root", "");

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return connection;  
}

/**
 * Insert record in database
 * @param request
 * @param connection
 */
private void insertRecord(HttpServletRequest request, Connection connection){   

            System.out.println("===== Received Record =====");

            PreparedStatement stmt;

            try {
                stmt = connection.prepareStatement("insert into users(Name,email,password) values(?,?,?)");
                stmt.setString(1, request.getParameter("name"));
                stmt.setString(2, request.getParameter("e-mail"));
                stmt.setString(3, request.getParameter("password"));

                int i= stmt.executeUpdate();

                if(i != 0){
                    System.out.println("data inserted !!!");
                }
                else{
                    System.out.println("data failed to insert");
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }

            finally{

                try {
                    connection.close();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
    }
}

and my android code is

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.NameValuePair;

import org.apache.http.message.BasicNameValuePair;

import com.example.messenger.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SignUp extends Activity {

Button register;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sign_up);

    register = (Button)findViewById(R.id.register);

    register.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.v("OnClick", "=== onClick ===");
            registerUser();

        }
    });
}

@SuppressLint({ "NewApi", "NewApi", "NewApi" })
private void registerUser(){


        String name= ((EditText)findViewById(R.id.name)).getText().toString();
        String email= ((EditText)findViewById(R.id.email)).getText().toString();
        String password= ((EditText)findViewById(R.id.password)).getText().toString();

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("name", name));
        nameValuePairs.add(new BasicNameValuePair("email", email));
        nameValuePairs.add(new BasicNameValuePair("password", password));


        try {

            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            URL url=new URL("http://192.168.1.10:8080/DBservlet/DBServlet");
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            con.setDoOutput(true);
            con.setRequestMethod("POST");
            DataOutputStream dos=new DataOutputStream(con.getOutputStream());
            dos.writeBytes(name);
            dos.flush();
            dos.close();

}  catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}

}

The problem is that when I run the ‘Servlet’ I get output ‘Servlet Started First Time’ which is OK but when I run my android client and write some data in EditText fields and press register button nothing happens. ‘Servlet’ receive nothing. Please let me know how can I solve this problem.

Regards,

  • 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-14T13:45:40+00:00Added an answer on June 14, 2026 at 1:45 pm

    Your servlet looks functionally okay (design-technically not, but that’s a different matter). Your Android side looks however definitely not good.

    This,

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("name", name));
    nameValuePairs.add(new BasicNameValuePair("email", email));
    nameValuePairs.add(new BasicNameValuePair("password", password));
    

    is how you would be preparing request parameters before sending the HTTP request using the Android-builtin Apache HttpClient API.

    But here,

    URL url=new URL("http://192.168.1.10:8080/DBservlet/DBServlet");
    HttpURLConnection con = (HttpURLConnection)url.openConnection();
    con.setDoOutput(true);
    con.setRequestMethod("POST");
    DataOutputStream dos=new DataOutputStream(con.getOutputStream());
    dos.writeBytes(name);
    dos.flush();
    dos.close();
    

    you’re however sending the HTTP request using the Java SE builtin URLConnection API instead. And then actually in a bad way; you’re not sending a proper application/x-www-form-urlendocded query string, but instead a single string value in the platform default encoding. Also the DataOutputStream makes completely no sense in this context, you’re not creating a .dat file at all. This feels too much like a “roseindia.net-snippet”. If you were indeed reading that terrible site, please put it in a lifetime blacklist from now on.

    It look like that you’re mixing different approaches of sending a HTTP POST request in Android.

    1. Using HttpClient API
    2. Using URLConnection API

    You should choose the one or the other and stick to it. The above links point to concrete examples in flavor of a Stack Overflow answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to send some data from and Android application to a
I'm trying to send some data from the server to the client, but the
I'm trying to send some data from a C++ server to a C# client.
I'm trying to send some data from c++ to java using JNI. In c++
I am trying to get some data from the user and send it to
I am trying to get some data from the user and send it to
I need to send some data from my Android device to my server. I
i'm trying to send an http request from an Android app to a Java
So i'm trying to send some data from my c# server to my gcm
I am trying to send data from an Android app to a PHP file

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.