package gastostarjetaServlet;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import m05bBD.m05bGastosTarjeta;
import m05bClasses.m05bFileLog;
import m05bGastosTarjetaEJB.m05GastosTarjeta;
import m05bGastosTarjetaEJB.m05GastosTarjetaHome;
import m05bGastosTarjetaEJB.m05GastosTarjetaPK;

public class m05bGenerarGastosTarjetaServlet extends javax.servlet.http.HttpServlet{
	m05bFileLog miLog = new m05bFileLog();
    /**
 * Process incoming HTTP GET requests
 *
 * @param request Object that encapsulates the request to the servlet
 * @param response Object that encapsulates the response from the servlet
 */
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {

	performTask(request, response);

}
/**
 * Returns the servlet info string.
 */
public String getServletInfo() {

	return super.getServletInfo();

}
/**
 * Initializes the servlet.
 */
public void init() {
	// insert code to initialize the servlet here

}
/**
 * Process incoming requests for information
 *
 * @param request Object that encapsulates the request to the servlet
 * @param response Object that encapsulates the response from the servlet
 */
public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) {

	try

	{

		String codigo;
		String sURL = "";
		HttpSession session = request.getSession(true);
        codigo = new String(request.getParameter("Codigo"));

		try{
			m05GastosTarjetaPK primaryKey = new m05GastosTarjetaPK();
			primaryKey.gastar_control = codigo;
			InitialContext ctx2 = new InitialContext();
			m05GastosTarjetaHome gastostarjetaEBHome = (m05GastosTarjetaHome) ctx2.lookup("m05GastosTarjeta");
			m05GastosTarjeta gastostarjetaEB = gastostarjetaEBHome.findByPrimaryKey(primaryKey);
			m05bGastosTarjeta gastostarjeta = gastostarjetaEB.getGastosTarjeta();

			session.setAttribute("m05bGastosTarjeta", gastostarjeta);

	    	sURL = response.encodeRedirectURL("m05bGastosTarjetaJsp.jsp?Codigo=" + codigo );
		    RequestDispatcher rd;
		    rd = request.getRequestDispatcher(sURL);
		    try{
		      rd.forward(request, response);
	    	}
		    catch (ServletException e){
		    	response.sendRedirect("m05bError.jsp");
		    }
		} catch (Exception SQLException) {
			response.sendRedirect("m05bError.jsp");
		}

	}
	catch(Throwable theException)
	{
		try {
			response.sendRedirect("m05bError.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			miLog.addTraza(e);
		}
	}
}

}
