我目前正在製作Captive Portal。現在我遇到了一個問題,我希望你們可以幫助我。我想使用JSON(數據庫連接)創建Java Servlet,但我不知道如何將JSON與Java結合。我在網上搜索了我的問題的答案,但沒有成功。我不知道該如何開始。使用JSON的Java servlet - 連接數據庫和登錄認證
其他信息:
- 外部服務器可以訪問由URL
- 我使用Eclipse進行了Java Servlet
難道我還需要使用SQL當我接近我的數據庫(服務器)
在此先感謝!
我有以下3個Java servlet。這些不完整:
我可能必須與第一個Servlet進行連接。第二個是用於身份驗證。但我不確定。你能給我解釋一下這些servlet的用途嗎?或可以做到?
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 {
\t enum BLServlet_Request {
\t \t Unknown,
\t \t User
\t };
\t private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BLServlet() {
super();
}
\t /**
\t * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
\t */
\t protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
\t dispatchRequest(request, response);
\t }
\t /**
\t * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
\t */
\t protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
\t }
\t
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);
\t switch (_CurrentRequest) {
\t \t \t case User:
\t \t \t \t rc = new BLRequestUser().handle(request, response);
\t \t \t \t break;
\t \t \t case Unknown:
\t \t System.out.println("BL servlet Context: received unknown request [" + s_Request + "]");
\t \t \t \t break;
\t }
\t
\t return rc;
}
\t
static BLServlet_Request requestFromURI(final String _uri) {
\t if (_uri.equals("/BLServlet/User")) {
\t \t return BLServlet_Request.User;
\t } \t \t
\t \t return BLServlet_Request.Unknown;
}
static String requestAsString(final BLServlet_Request _request) {
\t switch (_request) {
\t \t case User:
\t \t \t return "User";
\t \t case Unknown:
\t \t \t return "Unknown";
\t }
\t \t return "Unknown";
}
\t
\t
}
package BL;
import java.io.IOException;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
import javax.servlet.http.HttpServletResponse;
public class BLUser {
\t
\t private String Id = "";
\t private String Surname = "";
\t public BLUser(String s_Id) {
\t \t Id = s_Id;
\t \t if (s_Id.equals("1")) {
\t \t \t Surname = "Doe";
\t \t }
\t }
\t
\t public void toJson(HttpServletResponse response) throws IOException {
\t \t JsonGenerator _JsonGenerator = Json.createGenerator(response.getWriter());
\t \t
\t \t _JsonGenerator
\t \t \t .writeStartObject()
\t \t \t \t .write("id", Id)
\t \t \t \t .write("surname", Surname)
\t \t \t .writeEnd();
\t \t _JsonGenerator.close();
\t }
}
package BL;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BLRequestUser {
\t public BLRequestUser() {
\t }
\t
\t public int handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
\t \t String s_UserId = request.getParameter("id");
\t \t if (s_UserId == null) {
\t \t \t response.setStatus(400);
\t \t }
\t \t else {
\t \t \t response.setStatus(200);
\t \t \t response.setContentType("application/json");
\t \t \t BLUser _BLUser = new BLUser(s_UserId);
\t \t \t _BLUser.toJson(response);
\t \t }
\t \t
\t \t return 0;
\t }
}
我發現了以下內容,但我不知道如何應用它,你能幫我或給我任何提示嗎? - PS。我很新編程 - 請參閱以下鏈接:http://restsql.org/examples/java/example-restsql.properties – Hakan