sxymvc2

1.login.jsp

 

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <form action="LoginServlet" method="post">
        用户名 :<input type="text" name="username"><br>
        密码 :<input type="text" name="password"><br>
        <input type="submit" value="提交">
    </form>
    ${error }
  </body>
</html>

 2.welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'weclome.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
   welcome my friend <B>${username}</B> ! <br>
  </body>
</html>

3.LoginServlet.java

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.LoginDao;

public class LoginServlet extends HttpServlet {

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        LoginDao loginDao = new LoginDao();
        boolean isExist = loginDao.login(username, password);
        

        if(isExist){
            request.setAttribute("username",username);
            request.getRequestDispatcher("jsp/weclome.jsp").forward(request, response);
        }else{
            request.setAttribute("error","用户名或者密码输入错误");
            request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
            
        }
        
    }

    
}

4.LoginDao.java

 

package com.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class LoginDao {
    public boolean login(String username,String password){
        boolean isExist=false;
        Connection coon=null;
        Statement sta = null;
        ResultSet re = null;
        String url="jdbc:oracle:thin:@localhost:1521:inspur";
        String name="scott";
        String pass="tiger";
        try {
            
            //加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //建立连接
            coon=DriverManager.getConnection(url,name,pass);
            //创建Statement对象,执行sql
            sta=coon.createStatement();
            String sql="select * from loginuser where username = '"+username+"' and password ='"+password+"'";
            re=sta.executeQuery(sql);
            //处理结果
            if(re.next()){
                isExist=true;
            }
            //释放资源
            re.close();
                sta.close();
                coon.close();
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
        return isExist;
    }

    

}

 5.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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">
  <display-name></display-name>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.servlet.LoginServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
posted @ 2018-05-25 14:44  helloWorldhelloWorld  阅读(129)  评论(0)    收藏  举报