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;
}
}