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 <input type="text" name="name"/><br/>
Password <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>
- jsp&servlet
- Jsp&servlet
- jsp/servlet
- JSP Servlet
- servlet&JSP
- jsp/Servlet
- jsp-servlet
- servlet & jsp
- jsp---->servlet
- servlet/jsp
- jsp+servlet
- jsp/servlet
- Servlet+JSP
- jsp/servlet
- Jsp&Servlet
- jsp/Servlet
- Servlet+JSP
- JSP-servlet
- JDBC编程之数据库基本操作
- [LeetCode]289. Game of Life
- IOS开发中NSRunloop跟NSTimer的问题
- 12C-OCP升级1z-060-006
- C++全局常量与变量的定义方法
- jsp+servlet
- delphi httpserver 使用方法
- JAVA反射机制
- Retrofit 2.0:有史以来最大的改进
- 快速了解Log4J
- jquery的几种ajax方式
- linux命令(chown、find)
- 做人做事和个人修养
- [Ubuntu] 'Super'&'Win' Crash