Servlet login function implementation

Source: Internet
Author: User

(1) create a dynamic login page

//登陆界面package com.lc;import javax.servlet.http.*;import java.io.*;public class Login extends HttpServlet{public void doGet(HttpServletRequest req,HttpServletResponse res){//业务逻辑try{//解决中文乱码res.setContentType("text/html;charset=gbk");PrintWriter pw = res.getWriter();//返回登陆界面pw.println("(2) create a servlet that accepts Login

//登陆界面package com.lc;import javax.servlet.http.*;import java.io.*;public class Logincl extends HttpServlet{public void doGet(HttpServletRequest req,HttpServletResponse res){//业务逻辑try{//解决中文乱码//接受用户名和passwordString u = req.getParameter("username");String p = req.getParameter("passwd");//验证if(u.equals("lc") && p.equals("123")){//合法的情况 跳转到Welcomeres.sendRedirect("welcome");//这里跳转的仍是servlet的url}else{//不合法的情况//跳转res.sendRedirect("login");//跳转的是要到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

//登陆界面package com.tsinghua;import javax.servlet.http.*;import java.io.*;public class Welcome extends HttpServlet{public void doGet(HttpServletRequest req,HttpServletResponse res){//业务逻辑try{//解决中文乱码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) execution result:





Servlet login function implementation

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.