java web分层之——3层架构

3层架构,JSP+servlet+业务逻辑层

login.jsp

<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<HTML>
<HEAD>
<TITLE>质保中心</TITLE>
</HEAD>
<BODY>
<span id="tips"></span>
<form name="loginForm" action="doLogin" method="post">
用户名:<input type="text"  id="username" name="username">
密码:<input type="password" id="password" name="password">
<hr>
<input type="button" value="提交" onclick="ok()">
</form>
</BODY>
<script language="javascript">
function ok(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;

    if(username == "" || password == ""){
     // alert("用户名或密码不能为空!");
      document.getElementById("tips").innerHTML="<font color='red'>用户名或密码不能为空!</font>";
    }else{
        document.loginForm.submit();
    }
}
</script>
</HTML>

 

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_2_5.xsd"
   version="2.5"> 

  <servlet>
    <servlet-name>DoLoginServlet</servlet-name>
    <servlet-class>control.DoLoginServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>DoLoginServlet</servlet-name>
    <url-pattern>/doLogin</url-pattern>
  </servlet-mapping>
 
</web-app>

 

DoLoginServlet.java

package control;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.UserService;

public class DoLoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String encoding = "GBK";
		request.setCharacterEncoding(encoding);
		response.setCharacterEncoding(encoding);

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

		UserService s = new UserService();
		boolean isLoginOk = s.login(userName, password);

		if (isLoginOk) {
			request.getRequestDispatcher("success.jsp").forward(request, response);
		} else {
			request.getRequestDispatcher("fail.jsp").forward(request, response);
		}
	}

	
}

 

业务逻辑层 UserService.java

package model;

public class UserService {
	
	
	public boolean login(String username,String password) {
		
		 return ("黄启庆".equals(username) && "123456".equals(password));	
	}
}

 

你可能感兴趣的:(Java Web)