Solved

Convert the login page to jsp

Posted on 2004-03-24
2
507 Views
Last Modified: 2013-11-23
Can someone help me convert the program below to jsp?

Program:

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;


public class LoginServlet extends HttpServlet {

  public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {sendLoginForm(response, false);
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        String password = request.getParameter("password");
        String userName = request.getParameter("staffid");
      //String adminName = request.getParameter("administrator");

     if (login(userName, password)){

              HttpSession session = request.getSession();
              String event = request.getParameter("staffid");
              if (event.equals("administrator")) {
                     response.sendRedirect("../,,/client/adminmainpage.jsp");
              } else {
                          response.sendRedirect("./../client/mainpage.jsp");
              }

      }

        else {
              sendLoginForm(response, true);
         }

     }
  private void sendLoginForm(HttpServletResponse response,boolean withErrorMessage)throws ServletException, IOException {

  response.setContentType("text/html");
  PrintWriter out= response.getWriter();
  out.println("<HTML>");
  out.println("<HEAD>");
  out.println("<TITLE>Login</TITLE>");
  out.println("</HEAD>");
  out.println("<body background=background.jpg style=background-image: url(../../client/images/background.jpg)><h1>");
  out.println("<CENTER>");

  if (withErrorMessage)
  out.println("Login failed. Please try again.<BR>");
   out.println("<h1><img src="+"../../client/images/header.gif"+">");
   out.println("</b></font></h1>");
   out.println("<table border=4 cellpadding=10 cellspacing=0 align=center bordercolor=#333399 bordercolordark=black bordercolorlight=#333399 width=409>");
   out.println("<tr><td width=379 bgcolor=#E6E6E6>");
   out.println("<table border=0 cellpadding=5 cellspacing=0 align="+"center"+">");
   out.println("<tr><td align=center><h3>");
   out.println("<font color="+"#0033CC"+">");
   out.println("<FORM METHOD=POST>");
   out.println("<font size=3>To log in enter your Staff ID and Password.</font></font><font color=#0000FF size=3>");
   out.println("</h3></td></tr></table>");
   out.println("<table border=0 cellpadding=5 cellspacing=0 align="+"center"+">");
   out.println("<tr><td><B> <font color=#333333> Staff ID</font></B></td>");
   out.println("<td>:</td>");
   out.println("<td><input type=text name=staffid </td></tr>");
   out.println("<tr><td><B>Password</B></td>");
   out.println("<td>:</td>");
   out.println("<td><input type=password name=password size=20>"+"                </table><table border=0 cellpadding=5 cellspacing=0 align="+"center"+">");
   out.println("<tr><td align=center><input type=submit Name=\"login\" value=\"     OK      \">"+"&nbsp;&nbsp;&nbsp;&nbsp;"+"<input type=reset value=\"Cancel\">"+"</td></tr>");
   out.println("</table></td></tr></table>");
   out.println("<center><font size=2 color=#A52A2A>");
   out.println("<br><marquee align=middle width=450 height=19><B>"
                    +"! Thank You For Doing the Feedback.</B></marquee>");
   out.println("</font></center></strong></BODY></HTML>");
}



 boolean login(String userName, String password) {
       try{
             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
             Connection con =DriverManager.getConnection("jdbc:odbc:StudFdBk");
             System.out.println("got connection");
         System.out.println("Hello "+ userName);
         System.out.println("Hello "+ password);
             Statement s = con.createStatement();
             String sql = "SELECT STAFFID, PASSWORD FROM Staff " +
                          "WHERE StaffID = '"+ userName + "' "+
                          "AND Password = '"+ password + "' ";
             ResultSet rs = s.executeQuery(sql);
             if (rs.next()) {
                   rs.close();
                   s.close();
                   con.close();
                   return true;
             }

             rs.close();
             s.close();
             con.close();
       }
       catch (ClassNotFoundException e) {
              System.out.println(e.toString());
       }
       catch (SQLException e) {
              System.out.println(e.toString());
       }
       catch(Exception e){
             System.out.println(e.toString());
       }
       return false;
  }
}
0
Comment
Question by:Smiley31
  • 2
2 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 100 total points
ID: 10674569
First off, why do you want to do that? The result would be more or less the same.

Secondly, the JSP would be ultimately compiled into a Servlet only.

Still, if you want to do it, then you can do it:

- Remove the class/ methods.

- Simply use the tags <HTML>, etc instead of out.println ( "HTML" ), etc

- wherever some Java logic is required, enclose the Java code in <% %>
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10720802
Why a B :-(

You could've asked for more assistance if you needed it.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to implement Singleton Design Pattern in Java.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question