Solved

Convert the login page to jsp

Posted on 2004-03-24
2
509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

730 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