使用JSON的Java servlet - 连接数据库和登录身份验证

时间:2015-01-09 12:38:28

标签: java json eclipse servlets

我目前正在制作一个强制门户网站。现在我遇到了一个问题,我希望大家可以帮助我。我想使用JSON(数据库连接)创建Java Servlet,但我不知道如何将JSON与java结合使用。我在网上搜索了我的问题的答案,但没有成功。我不知道如何开始它。

其他信息:

  • 外部服务器可通过网址
  • 访问
  • 我正在使用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 {

	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 个答案:

答案 0 :(得分:1)

我想也许你可能会在这里混淆一​​些事情。 JSON(Javascript Object Notatation)是一种序列化对象的方法。通常,当您在浏览器中运行某些Javascript并调用服务器来获取某些数据时,将使用此方法。数据将以JSON格式到达。 (精简格式,比XML更轻量级)

当您的服务器端Java想要与数据库通信(假设SQL数据库)时,您需要使用JDBC。 (Java数据库连接)。这是一个API。

身份验证是另一回事。你可以"硬代码"一些凭据进入Servlet,如果这是足够的,但根据您的需要,您可能需要提示用户提供凭据,并使用这些凭据来访问您的数据库。

答案 1 :(得分:0)

JSON端{p> JSP用于发送信息,Servlate结束用于获取信息,即{}。}这就是它,它不会与您的数据库通信,只是在JSPServlet之间传递请求和响应。 Read more