<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns="http://xmlns.jcp.org/xml/ns/javaee"

    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

    version="3.1">

    <display-name>LabWebServletJdbc</display-name>

   

<!--   設定告訴系統我的spring設定檔在哪哩,如果名字取成預設名稱applicationContext.xml就可以省略下四行 -->

    <context-param>

    <param-name>contextConfigLocation</param-name><!-- 這是固定的 -->

    <param-value>/WEB-INF/beans.xml</param-value><!-- 這是檔案路徑 -->

    </context-param>

   

    <!-- 設定好contextConfigLocation之後,她才能想辦法把ContextLoaderListener new出來 -->

    <listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

   

   

    <resource-ref>

       <res-ref-name>jdbc/xxx</res-ref-name>

       <res-type>javax.sql.DataSource</res-type>

       <res-auth>Container</res-auth>

       <res-sharing-scope>Shareable</res-sharing-scope>

    </resource-ref>

      

    <welcome-file-list>

       <welcome-file>index.html</welcome-file>

       <welcome-file>index.htm</welcome-file>

       <welcome-file>index.jsp</welcome-file>

       <welcome-file>default.html</welcome-file>

       <welcome-file>default.htm</welcome-file>

       <welcome-file>default.jsp</welcome-file>

    </welcome-file-list>

</web-app>



<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 

    <!-- 以下是DriverManagerDataSource的寫法,被Jndi取代 -->

    <!-- <bean id="dataSource" -->

    <!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource"> -->

    <!-- <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> -->

    <!-- <property name="url" value="jdbc:sqlserver://localhost:1433;database=java"></property> -->

    <!-- <property name="username" value="sa"></property> -->

    <!-- <property name="password" value="passw0rd"></property> -->

    <!-- </bean> -->

 

    <!-- 我是JNDI -->

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

       <property name="jndiName" value="java:comp/env/jdbc/xxx"></property>

    </bean>

 

 

    <bean id="CustomerDAOJdbc" class="model.dao.CustomerDAOJdbc">

       <constructor-arg ref="dataSource"></constructor-arg>

    </bean>

 

    <bean id="CustomerService" class="model.CustomerService">

       <constructor-arg ref="CustomerDAOJdbc"></constructor-arg>

    </bean>

 

    <bean id="ProductDAOJdbc" class="model.dao.ProductDAOJdbc">

       <constructor-arg ref="dataSource"></constructor-arg>

    </bean>

 

    <bean id="ProductService" class="model.ProductService">

       <constructor-arg ref="ProductDAOJdbc"></constructor-arg>

    </bean>

</beans>



package controller;

 

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

 

import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import model.CustomerBean;

import model.CustomerDAO;

import model.CustomerService;

import model.CustomerServiceOld;

 

import org.springframework.context.ApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

 

@WebServlet(urlPatterns = {"/secure/login.controller"})

public class LoginServlet extends HttpServlet {

    private CustomerService customerService; // = new CustomerService();

 

    @Override

    public void init() throws ServletException {

       // 原本寫法:

       // CustomerDAO dao = new CustomerDAOJdbc();

       // customerService = new CustomerService();

       System.out.println("LoginServlet測試");

       ServletContext application = this.getServletContext();

       ApplicationContext context = WebApplicationContextUtils

              .getWebApplicationContext(application);

       this.customerService = (CustomerService) context.getBean("CustomerService");

    }

 

    @Override

    protected void doGet(HttpServletRequest request,

           HttpServletResponse response) throws ServletException, IOException {

       String username = request.getParameter("username");

       String password = request.getParameter("password");

 

       Map<String, String> errors = new HashMap<String, String>();

       request.setAttribute("error", errors);

 

       if (username == null || username.trim().length() == 0) {

           errors.put("username", "名稱");

       }

       if (password == null || password.trim().length() == 0) {

           errors.put("password", "密碼");

       }

       if (errors != null && !errors.isEmpty()) {

           request.getRequestDispatcher("/secure/login.jsp").forward(request,

                  response);

           return;

       }

       // 呼叫MODEL

       CustomerBean bean = customerService.login(username, password);

       // 依據MODEL顯示結果

 

       if (bean == null) {// 代表帳號輸入錯誤

           errors.put("password", "帳號不存在");

           request.getRequestDispatcher("/secure/login.jsp").forward(request,

                  response);

       } else {

           request.getSession().setAttribute("user", bean);

           response.sendRedirect(request.getContextPath() + "/index.jsp");

 

       }

    }

 

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)

           throws ServletException, IOException {

       this.doGet(req, resp);

 

    }

 

}



 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 乙方 的頭像
    乙方

    學習筆記專區

    乙方 發表在 痞客邦 留言(0) 人氣()