| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 
 | package gpi.servlet;
 
import gpi.session.login.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.util.*;
import java.io.*;
import javax.ejb.EJBException;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
 
/**
 * @author arnaud
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class LoginGPI extends HttpServlet {
 
	private myLoginHome myloginHome = null;
	private myLogin mylogin = null;
	private static String LOGIN_SCREEN="/login";
	private static String LOGIN_ERROR_SCREEN="/loginerror";
	private static String SUCCESS_SCREEN="/loginsuccess";
 
 
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		initLogin();
		//TODO Method stub generated by Lomboz
	}
 
	private void initLogin(){
		System.out.println("Access to login");
		try{
			myloginHome = getHome();
		}
		catch(Exception e){
			System.out.println("Access failed");
		}
	}
 
	private myLoginHome getHome() throws NamingException{
		Object result=getContext().lookup(myLoginHome.JNDI_NAME);
		return ((myLoginHome)PortableRemoteObject.narrow(result,myLoginHome.class));
	}
 
	private InitialContext getContext()throws NamingException{
		Hashtable props = new Hashtable();
		props.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(InitialContext.PROVIDER_URL,"jnp://127.0.0.1:1099");
		InitialContext initialContext = new InitialContext(props);
		return initialContext;
	}
 
	public void destroy() {
		super.destroy();
		//TODO Method stub generated by Lomboz
	}
 
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println(" YO Checking login");
		//TODO Method stub generated by Lomboz
		processRequest(request,response);
	}
 
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//System.out.println("Checking login");
		System.out.println("YO");
		//TODO Method stub generated by Lomboz
		processRequest(request,response);
	}
 
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("YO enter with error2");
 
		HttpSession session = request.getSession(true);
		String useraction = request.getParameter("useraction");
		String buildurl=null;
		String temp = null;
		int test =0;
 
		if (useraction==null){
			buildurl = LOGIN_SCREEN;
			System.out.println("no action user ");
		}
		else
		{
			if (useraction.equals("dovalidation"))
			{
				String username = request.getParameter("username");
				temp = username;
				String password = request.getParameter("password");
				if ((username==null)||(!loginuser(username,password,session)))
				{
					buildurl = LOGIN_ERROR_SCREEN;
					System.out.println(" bad password");
				}
				else
				{
					buildurl = SUCCESS_SCREEN;
					System.out.println(" success screen ");
				}
			}
		}
 
		if (buildurl == LOGIN_SCREEN)
		{
			if (test!=1)
			{
				displayloginscreen(response);
				test = 1;
			}
			else
			{
				displaysuccessscreen(response,temp);
			}
			System.out.println(" login");
		}
		else if (buildurl == SUCCESS_SCREEN)
		{
			displaysuccessscreen(response,temp);
			System.out.println("sucess");
		}
		else if (buildurl == LOGIN_ERROR_SCREEN)
		{
			displayerrorscreen(response);
			System.out.println("error");
		}
		System.out.println(" merde");
	}
 
	private void displayloginscreen(HttpServletResponse response) throws IOException
	{
		System.out.println("Entering AccessController.displayloginscreen()");
		response.setContentType("text/htlm");
		PrintWriter out = response.getWriter();
		out.println("<html><title>MyLogin Login</title>");
		out.println("<body><h2>Welcome to Mylogin</h2></body>");
		out.println("<form method=\"GET\">");
		displayLoginDataFields(out);
		out.println("</form></html>");
		if (out != null)out.close();
		System.out.println("Leaving AccessController.displayLoginScreen()");
	}
 
	private void displayerrorscreen(HttpServletResponse response) throws IOException
	{
		System.out.println("Entering AccessController.displayloginscreen()");
		response.setContentType("text/htlm");
		PrintWriter out = response.getWriter();
		out.println("<html><title>MyLogin ERROR</title>");
		out.println("<body><h2>Welcome to MyERROR</h2></body>");
		if (out != null)out.close();
		System.out.println("Leaving AccessController.displayLoginScreen()");
	}
 
	private void displayLoginDataFields(PrintWriter out)
	{
		out.println("<h3>Please enter your username and password:.");
		out.println("<table><tr>Username: <td><input name=\"username\" type=\"text\">");
		out.println("<tr><td>Password: <td><input name=\"password\" type=\"password\">");
		out.println("</table>");
		out.println("<input type=\"submit\" value=\"login\" name=\"loginbutton\">");
		out.println("<input type=\"reset\" name=\"resetButton\" value=\"reset\">");
		out.println("<input type=\"hidden\" name=\"useraction\" value=\"dovalidation\">");
	}
 
	private void displaysuccessscreen(HttpServletResponse response,String username)
	{
		try{
			System.out.println("Entering AccessController.displaysucessscreen()");
			response.setContentType("text/htlm");
			PrintWriter out = response.getWriter();
			out.println("<html><title>MyLogin Login</title>");
			out.println("<body><h2>Bravo");
			out.println(username);
			out.println("</h2></body>");
			out.println("<form method=\"get\">");
			out.println("</form></html>");
			if (out != null)out.close();
			System.out.println("Leaving AccessController.displaysucessScreen()");
		}catch (Exception e)
		{
			System.out.println("servlet failed");
		}
 
	}
 
	private boolean loginuser(String username,String password,HttpSession session){
		String userid = null;
 
		try{
			myLogin mylogin = myloginHome.create();
			userid = mylogin.checklogin(username,password);
			session.setAttribute("userID",userid);
		}catch(Exception e)
		{
			System.out.println("Exception in loginuser()");
		}
 
		if (userid !=null)
		{
			return (true);
		}
		else
		{
			return (false);
		}
	}
} | 
Partager