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.
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 ();
}
|