Servlet login and servlet Login

Source: Internet
Author: User

Servlet login and servlet Login

(1) create a dynamic Login page

// Log on to the package com. lc; import javax. servlet. http. *; import java. io. *; public class Login extends HttpServlet {public void doGet (HttpServletRequest req, HttpServletResponse res) {// business logic try {// solves Chinese garbled res. setContentType ("text/html; charset = gbk"); PrintWriter pw = res. getWriter (); // return to the logon interface pw. println ("(2) create a servlet that accepts Login

// Log on to the package com. lc; import javax. servlet. http. *; import java. io. *; public class Logincl extends HttpServlet {public void doGet (HttpServletRequest req, HttpServletResponse res) {// business logic try {// solve Chinese garbled characters // accept username and password String u = req. getParameter ("username"); String p = req. getParameter ("passwd"); // verify if (u. equals ("lc") & p. equals ("123") {// jump to Welcomeres. sendRedirect ("welcome"); // The servlet url is redirected here} else {// illegal // redirect res. sendRedirect ("login"); // jump to the servlet url} catch (Exception ex) {ex. printStackTrace () ;}} public void doPost (HttpServletRequest req, HttpServletResponse res) {this. doGet (req, res );}}

(3) successful login to the welcome page to jump

// Log on to the package com. tsinghua; import javax. servlet. http. *; import java. io. *; public class Welcome extends HttpServlet {public void doGet (HttpServletRequest req, HttpServletResponse res) {// business logic try {// solves Chinese garbled res. setContentType ("text/html; charset = gbk"); PrintWriter pw = res. getWriter (); pw. println ("hello welcome! ");} Catch (Exception ex) {ex. printStackTrace () ;}} public void doPost (HttpServletRequest req, HttpServletResponse res) {this. doGet (req, res );}}

(4) Compiling the configuration file web. xml

<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  version="3.0"  metadata-complete="true">  <display-name>Welcome to Tomcat</display-name>  <description>     Welcome to Tomcat  </description>  <!--JSP servlet mappings starts-->  <servlet>    <servlet-name>Login</servlet-name>    <servlet-class>com.lc.Login</servlet-class>  </servlet>   <servlet-mapping>    <servlet-name>Login</servlet-name>    <url-pattern>/login</url-pattern>  </servlet-mapping>  <servlet>    <servlet-name>Logincl</servlet-name>    <servlet-class>com.<span style="font-family:Arial, Helvetica, sans-serif;">lc</span>.Logincl</servlet-class>  </servlet>   <servlet-mapping>    <servlet-name>Logincl</servlet-name>    <url-pattern>/logincl</url-pattern>  </servlet-mapping>  <servlet>    <servlet-name>Welcome</servlet-name>    <servlet-class>com.lc.Welcome</servlet-class>  </servlet>   <servlet-mapping>    <servlet-name>Welcome</servlet-name>    <url-pattern>/welcome</url-pattern>  </servlet-mapping><!--JSP servlet mappings end--></web-app>

(5) running result:






Jsp + servlet + oracle for User Login

String username = request. getParameter ("username") in servlet ");
String password = request. getParameter ("password ");
// The parameter name corresponds to the actual jsp page input box name
Boolean right = new DBOperator (). MyQuery (username, password );
DBOperator is the custom class name MyQuery (). It is the method used in this class to connect to the database and verify the user name and password.
The MyQuery () method defined in DBOperator is as follows:
Public boolean MyQuery (String username, String password ){
Class. forName ("oracle. jdbc. driver. OracleDriver"). newInstance ();
String url = "jdbc: oracle: thin: @ 127.0.0.1: 1521: orcl ";
Connection con = DriverManager. getConnection (url, username, password );
PreparedStatement ps;
ResultSet rs;
String SQL = "select count (*) from tablename where username = '" + username + "' and password = '" + password + "'";
Rs = ps.exe cuteQuery (SQL );
If (rs. getInt (0) <= 0 ){
Return false
}
Return true;
}

JSP & Servlet programming: 1. Complete the functions of user registration, user logon, and successful login, including page implementation.

There are many such things on the internet,
<% @ Page language = "java" contentType = "text/html; charset = GB18030"
PageEncoding = "GB18030" %>
<% @ Page import = "java. util. *" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> logon page </title>
</Head>
<Body>
<Form name = "loginForm" method = "post" action = "judgeUser. jsp">
<Table>
<Tr>
<Td> User name: <input type = "text" name = "userName" id = "userName"> </td>
</Tr>
<Tr>
<Td> password: <input type = "password" name = "password" id = "password"> </td>
</Tr>
<Tr>
<Td> <input type = "submit" value = "login" style = "background-color: pink "> <input type =" reset "value =" reset "style =" background-color: red "> </td>
</Tr>
</Table>
</Form>
</Body>
</Html>

<% @ Page language = "java" contentType = "text/html; charset = GB18030"
PageEncoding = "GB18030" %>
<% @ Page import = "java. util. *" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Hea ...... remaining full text>

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.