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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:59:10+00:00 2026-05-27T03:59:10+00:00

I am developing a webapplication based on JSP. I have a servlet class :

  • 0

I am developing a webapplication based on JSP. I have a servlet class :

package managesystem;

import java.util.List;

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

import com.google.gson.Gson;

public class getUsernamesServlet extends HttpServlet{

/**
 * 
 */
private static final long serialVersionUID = 1L;

   public void doGet(HttpServletRequest req, HttpServletResponse resp){
      List<String> usernamesList = StudentManager.findAllUsernames();
      req.setAttribute("usernames", new Gson().toJson(usernamesList));

   }

}

My question is as follows : How do I check with Ajax if a usernames is still available (if it isn’t present in the list) ? How do I get the JSON information that the servlet writes to the request, in for example register.jsp using Ajax ?

Kind regards,

h4

  • 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-05-27T03:59:10+00:00Added an answer on May 27, 2026 at 3:59 am

    You need to write it to the response body instead of setting it as a request attribute.

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(new Gson().toJson(usernamesList));
    

    This way the Ajax request on this servlet retrieves a JSON response which can then be traversed the usual way using JavaScript.

    That said, if your sole purpose is to check if the username is available, then you can also approach this a bit differently. Instead of pumping the entire list of all user names over the network and doing the checking job in the JavaScript side, you could also just send the entered username as a request parameter to the servlet and let the DB do the checking job and return just a boolean true or false whether the usename is available. For example,

    String username = request.getParameter("username");
    boolean usernameAvailable = studentService.usernameAvailable(username);
    
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("usernameAvailable", usernameAvailable);
    
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(new Gson().toJson(data));
    

    with

    $.get('someservlet', { 'username': username }, function(data) {
        if (!data.usernameAvailable) {
            $('#somemessage').text('Username is not available, please choose another').show();
        }
    });
    

    That’s more bandwidth-efficient and less prone for leaking sensitive information.

    See also:

    • How to use Servlets and Ajax?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a small intranet based web application. I have YSlow installed and
I'm developing an ASP.NET forms webapplication using C#. I have a method which creates
I'm developing a web based application built on codeigniter which will also have a
I'm currently developing a Java-based web application on my MacBook. Most of my testing
I'm developing a J2EE-based web application on a SAP Netweaver Application Server. I have
I am developing an Eclipse/Equinox based OSGI WebApplication (using embedded Jetty) and using headless
Our team is currently developing a web application, in that we have a class
I am developing a web application based on JSF technology. I use Eclipse as
We are in the process of developing a .NET based IIS hosted web application
I am developing a web application which has Chart Controls. I have developed a

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.