WebServer : Login Screen
http://docs.oracle.com/javaee/6/tutorial/doc/gjiie.html
http://www.tutorialspoint.com/servlets/servlets-form-data.htm (All sorts of form elements)
http://mrbool.com/using-html-forms-with-servlets/28335
http://www.javatpoint.com/servlet-http-session-login-and-logout-
http://www.javatpoint.com/servlet-http-session-login-and-logout-example
http://tutorials.jenkov.com/java-servlets/index.html
doGet()
http://www.tutorialspoint.com/servlets/servlets-form-data.htm (All sorts of form elements)
http://mrbool.com/using-html-forms-with-servlets/28335
http://www.javatpoint.com/servlet-http-session-login-and-logout-
http://www.javatpoint.com/servlet-http-session-login-and-logout-example
http://tutorials.jenkov.com/java-servlets/index.html
doGet()
- small amount of data
- insensitive data like a query has to be sent as a request.
doPost()
- large amount of has to be sent.
- sensitive data (Examples are sending data after filling up a form or sending login id and password.)
Note :
Following methods of the HttpServletRequest interface that enable you to authenticate users for a web application programmatically:
login-An alternative to specifying form-based authentication in an application deployment descriptor.
logout- which allows an application to reset the caller identity of a request.
Forward():
- Executed in the server side.
- The request is transfer to other resource within same server.
- Any Client
- The request is shared by the target resource.
- It can be used within server.
- We cannot see forwarded message
- faster
- It is declared in RequestDispatcher interface.
- Only Response from Server
sendRedirect():
- Executed in the client side.
- The request is transfer to other resource to different server.
- Only with HTTP clients.
- New request is created for the destination resource.
- It can be used within and outside the server.
- We can see redirected address, it is not transparent.
- slower
- It is declared in HttpServletResponse.
Include()
Forward() + Client Side info()
Application Overview:
Index.html > Login.html > (Pass)Welcome.html or (fail) Login.html
@WebServlet("/Login")
public class MyServerlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String sURL="jdbc:mysql://localhost:3306/mydb";
private static final String sUsername="root";
private static final String sPassword="password";
public MyServerlet1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List LRes=null;
RequestDispatcher rd = null;
DBHelper DBHelp=new DBHelper();
PrintWriter out=response.getWriter();
String username = request.getParameter("Usn");
String password = request.getParameter("Password");
LRes=DBHelp.SelectQuery(sURL,sUsername,sPassword,"SELECT * FROM mydb.login where UserID ='"+username+"' and Password='"+password+"';");
if(LRes.isEmpty()==false){
out.print("Login Successful");
HttpSession session=request.getSession();
session.setAttribute("name",username);
// response.sendRedirect(request.getContextPath() + "/WelcomeServlet.jsp");
rd = request.getRequestDispatcher("/WelcomeServlet.html");
rd.include(request, response);
//rd.forward(request, response); -- This can be used as well
LRes=null;
}
else{
out.print("Login failed");
response.sendRedirect(request.getContextPath() + "/Login.html");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Database helper Class :
public class DBHelper {
public List SelectQuery(String sURL,String sUsn,String sPassword,String sQuery){
ResultSet rs=null;
MapListHandler rstoList=new MapListHandler();
Map<String,Object> MapQuery=new HashMap<String,Object>();
List resList=null;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(sURL,sUsn, sPassword);
Statement st=(Statement) connection.createStatement();
rs=st.executeQuery(sQuery);
resList= rstoList.handle(rs);
rs.close();
st.close();
connection.close();
}catch(Exception e){
System.out.println("Failed to make connection!");
e.printStackTrace();
}
return resList;
}
}