0

I am currently in the process of making a Captive Portal. Right now I am encountering a problem, and I hope you folks could help me out. I want to make a Java Servlet with the use of JSON (database connection) but I don't know how to combine JSON with java. I have searched on the web for answers on my problem, but with no success. I don't know how to make a start with it.

Additional information:

  • external server is reachable by an url
  • I am using Eclipse to make the java servlet

Do I also need to use SQL when I approach my database(server)

Thanks in advance!

I've the following 3 Java servlets. These aren't complete:

I probably have to make the connection with the first Servlet. The second 2 are for authentication. But i'm not sure. Could you give me explanation what these servlets do? or can do?

package BL;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class BLServlet
 */
@WebServlet("/BLServlet/*")
public class BLServlet extends HttpServlet {

	enum BLServlet_Request {
		Unknown,
		User
	};

	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BLServlet() {
        super();
    }

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

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

	
    private int dispatchRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int rc = 0;
        
        HttpSession _HttpSession = request.getSession();
        
        String s_RequestURL = request.getRequestURL().toString();
        String s_RequestURI = request.getRequestURI();
        String s_ContextPath = request.getContextPath();

        String s_Request_Prefix = s_ContextPath;
        String s_Request = s_RequestURI.substring(s_Request_Prefix.length(), s_RequestURI.length());
        if (!s_Request.startsWith("/")) {
            s_Request = "/" + s_Request;
        }
        
        BLServlet_Request _CurrentRequest = requestFromURI(s_Request);
        
        System.out.println("BL servlet Context: " + s_ContextPath + " request URL: " + s_RequestURL + " URI: " + s_RequestURI + " Request: " + s_Request);
        
    	switch (_CurrentRequest) {
			case User:
				rc = new BLRequestUser().handle(request, response);
				break;
			case Unknown:
		        System.out.println("BL servlet Context: received unknown request [" + s_Request + "]");
				break;
    	}
    	
    	return rc;
    }
	

    static BLServlet_Request requestFromURI(final String _uri) {
    	if (_uri.equals("/BLServlet/User")) {
    		return BLServlet_Request.User;
    	}    		
		return BLServlet_Request.Unknown;
    }

    
    static String requestAsString(final BLServlet_Request _request) {
    	switch (_request) {
    		case User:
    			return "User";
    		case Unknown:
    			return "Unknown";
    	}
		return "Unknown";
    }
        	
	
}

package BL;

import java.io.IOException;

import javax.json.Json;
import javax.json.stream.JsonGenerator;
import javax.servlet.http.HttpServletResponse;

public class BLUser {
	
	private String Id = "";
	private String Surname = "";

	public BLUser(String s_Id) {
		Id = s_Id;
		if (s_Id.equals("1")) {
			Surname = "Doe";
		}
	}
	
	public void toJson(HttpServletResponse response) throws IOException {
		JsonGenerator _JsonGenerator = Json.createGenerator(response.getWriter());
		
		_JsonGenerator
			.writeStartObject()
				.write("id", Id)
				.write("surname", Surname)
			.writeEnd();
		_JsonGenerator.close();
	}
}

package BL;

import java.io.IOException;

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

public class BLRequestUser {

	public BLRequestUser() {
	}
	
	public int handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String s_UserId = request.getParameter("id");
		if (s_UserId == null) {
			response.setStatus(400);
		}
		else {
			response.setStatus(200);
			response.setContentType("application/json");
			BLUser _BLUser = new BLUser(s_UserId);
			_BLUser.toJson(response);
		}
		
		return 0;
	}
}

2 Answers 2

1

I think perhaps you might be confusing a few things here. JSON (Javascript Object Notatation) is a way to serialise objects. Typically, this would be used when you have some Javascript running in a browser, and you call a server to get some data. The data would arrive in JSON format. (A lean format, more lightweight than XML)

When your server-side Java wants to communicate with a database (Assuming an SQL database) you need to use JDBC. (Java DataBase Connectivity). This is an API.

Authentication is a different matter. You can "Hard Code" some credentials into the Servlet, if that is suffucient, but depending on your needs, you may need to prompt a user for credentials, and use these to access your database.

Sign up to request clarification or add additional context in comments.

1 Comment

I've found the following but i dont know how to apply it, could you help me or give me any tips? - PS. i'm pretty new to programming -- see following link: restsql.org/examples/java/example-restsql.properties
0

JSON is used at JSP end for sending information and Servlate end for taking information and viz. that's it, It will not communicate with your database it just pass request and response between JSP and Servlet. Read more

2 Comments

First of all i want to thank you for your answer. Is it possible to use JSON codes (with/in a Java servlet) to authenticate users?(login system)
Yes! you can you should use JSON will transfer data between your JSP so submit form on JSP and with the help of JSON post it to servlet and using servlet you can write business logic to authenticate user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.