Solved

Convert the login page to jsp

Posted on 2004-03-24
2
510 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

705 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