--> -->

JSP

 

Gson 라이브러리 다운로드

 

 

 ApiFindUserServlet

 

package com.slipp.user;

import java.io.IOException;
import java.io.PrintWriter;

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 com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@WebServlet("/api/users/find")
public class ApiFindUserServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String userId=req.getParameter("userId");
		if(userId==null){
			resp.sendRedirect("/");
			return;
		}
		
		UserDAO userDao =new UserDAO();
		User user=userDao.findByUserId(userId);
		if(user==null){
			return;
		}
		
		
		final GsonBuilder builder=new GsonBuilder();
		builder.excludeFieldsWithoutExposeAnnotation();
		final Gson gson=builder.create();
		
		
		
		String jsonData =gson.toJson(user);
		resp.setContentType("application/json;charset=utf-8");
		PrintWriter out =resp.getWriter();
		out.println(jsonData);
	}

	
	
}


 

 

 class User

package com.slipp.user;

import com.google.gson.annotations.Expose;
import com.slipp.PasswordMismatchException;
import com.slipp.db.Database;

public class User {
	@Expose
	private String userid;
	
	@Expose(serialize=false)
	private  String password;
	
	@Expose
	private String name;
	
	@Expose
	private String email;
	
	public User() {
	
	}
	
	public User(String userid, String password, String name, String email) {
		super();
		this.userid = userid;
		this.password = password;
		this.name = name;
		this.email = email;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public boolean matchPassword(String newPassword) {
		return this.password.equals(newPassword);
	}
	
	public static boolean login(String userid, String password) throws NotFoundUserException, PasswordMismatchException {
		UserDAO userDao=new UserDAO();
		User user=null;
		//User user =Database.findByUserId(userid);
		
		user=userDao.findByUserId(userid);
		
		if(user==null){
			throw new NotFoundUserException();
		}
		
		if(!user.matchPassword(password)){
			throw new PasswordMismatchException();
		}
		
		return true;
	}
	
	
	
	@Override
	public String toString() {
		return "User [userid=" + userid + ", password=" + password + ", name=" + name + ", email=" + email + "]";
	}


	
	
	
}


 

 

출력

 

 

{

  • userid: "qqq",

  • name: "김길동",

  • email: "aaa@aaa"

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

하루라도 선을 생각하지 않으면 모든 악이 스스로 일어난다. -장자

댓글 ( 0)

댓글 남기기

작성