Solved

Convert the login page to jsp

Posted on 2004-03-24
2
505 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:
mayankeagle earned 100 total points
Comment Utility
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:mayankeagle
Comment Utility
Why a B :-(

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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 …

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now