jsp+servlet

来源:互联网 发布:网络相亲平台 编辑:程序博客网 时间:2024/06/16 06:25

package login;

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 loginServlet extends HttpServlet {

 /**
  * Constructor of the object.
  */
 public loginServlet() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  out.println("<HTML>");
  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println("  <BODY>");
  out.print("    This is ");
  out.print(this.getClass());
  out.println(", using the GET method");
  out.println("  </BODY>");
  out.println("</HTML>");
  out.flush();
  out.close();
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html;charset=GB2312");//设置响应的MIME类型。
  java.io.PrintWriter out = response.getWriter();
         
  String type=request.getParameter("typeId");
  if (type.trim().equals("login")) {
  String name=request.getParameter("name");//获取视图里的name
  String pas=request.getParameter("pas");//获取视图里的password
  model newModel=new model();//调用模型
  newModel.setName(name);
  newModel.setPas(pas);
  if(newModel.login())
  {
  out.print("script Lanuage='JavaScript'>window.alert('登录成功!')</script>");
  }
  else {
  out.print("script Lanuage='JavaScript'>window.alert('登录失败!')</script>");
  }
  }
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }

}

 

 

/**
 * 2010-4-19
 * model.java
 * author:Xiangzi
 */
package login;

public class model {
 
 private String name=null;
 private String pas=null;
 
 public void setName(String name)
 {
  this.name=name;
 }
 
 public String getName() {
  return name;
 }
 
 public void setPas(String pas)
 {
  this.pas=pas;
 }
 
 public String getPas() {
  return pas;
 }
 
 public boolean login()
 {
  if ((name.trim().equals("admin"))&&(pas.trim().equals("1234"))) {
   return true;
  }
  else {
   return false;
  }
 }
}

 

 

 

 

index.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 'index.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">
 
  </head>
 
 

</script>
  <body>
    <form action="loginServlet" method="post">
  <div>
  Name&nbsp;<input type="text" name="name"/><br/>
  Password&nbsp;<input type="text" name="pas"/><br/>
  <input type="submit" value="Login"/>
  <input type="hidden" value="login" name="typeId"/>
  </div>
  </form>
  </body>
</html>

 

 

 

 

 

web.xml

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 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">
  <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>login.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>

 

 


 

0 0