java web分层之——2层架构

2层架构,JSP+Servlet

login.jsp

<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<HTML>
<HEAD>
<TITLE>2层架构</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>的值必须以/开头--> 
   <url-pattern>/doLogin</url-pattern>
  </servlet-mapping>
 
</web-app>

 

 

control.DoLoginServlet.java

package control;

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;

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");
		  
		  if("黄启庆".equals(userName) && "123456".equals(password)){
			  request.getRequestDispatcher("success.jsp").forward(request, response);
		  }else{
			  request.getRequestDispatcher("fail.jsp").forward(request, response);
		  }
	}

	
}

 

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