Jersey @Context HttpServeltRequest webservices Restful JAX-RS tutorial


@Context

This annotation is used to inject information into a class field, bean property or method parameter.

Code

Controller: RegisterProcess.java

package com.candidjava.webservices.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.candidjava.webservices.bean.User;

@Path("/User")
public class RegisterProcess {
	@GET
	@Path("/RegisterProcess")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getRegisterDetails(@Context HttpServletRequest request,
			@Context HttpServletResponse response) {
		String username = request.getParameter("txt_username");
		String password = request.getParameter("txt_password");
		System.out.println(username);
		System.out.println(password);

		User user = new User(username, password);

		return Response.ok().status(200).entity(user).build();

	}
	// *************************************************

}


Bean class: User.java

package com.candidjava.webservices.bean;

public class User {

	private String username;
	private String password;

	// *********************************

	public User(String username, String password) {

		super();
		this.username = username;
		this.password = password;

	}

	// *********************************

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

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

}

Screenshot

            

            

Download

            Jersey @context maven zip

            Jersey @context war






Related Post

Comments


©candidjava.com