Complete JSP program for user login verification

Source: Internet
Author: User

Public String validateUser (String inputUserid, String inputPwd)
Throws SQLException
{
String returnString = null;
String dbUserid = "userid"; // User Name of the database
String dbPassword = "password"; // Database password
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
Connection con = DriverManager. getConnection ("jdbc: odbc: myDriver", dbUserid, dbPassword );
Statement stmt = con. createStatement ();
String SQL = "select USERID from USERTABLE where USERID = '" + inputUserid + "' and PASSWORD = '" + inputPwd + "';";
ResultSet rs = stmt.exe cuteQuery (SQL );
If (rs. next ())
{
ReturnString = rs. getString ("USERID ");
}
Stmt. close ();
Con. close ();
Return returnString;
}

How to Set application variables

The application variable is a global variable that is valid for a web application and is useful when saving some global valid values. Unlike session variables, application variables can be accessed using application. getAttribute () and application. setAttribute.

In JSP, the application variable can be automatically referenced to indicate the current web application.

Synchronization is required when the application variable is referenced. In addition, you need to test whether an application attribute already exists. Please carefully study the following code. below, you have set an application attribute named "change" to save an integer value. The method for using the application variable in servlet is the same as that in the following code. However, you must first declare an application as follows:

ServletContext application = getServletContext ();

The following function is a piece of env. Java. You can see how to use the application variable in servlet.


Welcome, visitor:

How to Prevent IE from caching jsp files

1. You can use the methods provided by Java in jsp or servlet.

2. Use HTML tags, as shown below:

Jsp functions similar to htmlencode functions in asp

Public static String asHTML (String text)
{
If (text = null)
Return "";
StringBuffer results = null;
Char [] orig = null;
Int beg = 0, len = text. length ();
For (int I = 0; I <len; ++ I)
{
Char c = text. charAt (I );
Switch (c ){
Case 0:
Case '&':
Case '<':
Case '> ':
Case '"':
If (results = null)
{
Orig = text. toCharArray ();
Results = new StringBuffer (len + 10 );
}
If (I> beg)
Results. append (orig, beg, I-beg );
Beg = I + 1;
Switch (c)
{
Default: // case 0:
Continue;
Case '&':
Results. append ("&");
Break;
Case '<':
Results. append ("<");
Break;
Case '> ':
Results. append ("> ");
Break;
Case '"':
Results. append (""");
Break;
}
Break;
}
}
If (results = null)
Return text;
Results. append (orig, beg, len-beg );
Return results. toString ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.