Java Web: 用户登陆功能的实现

来自CloudWiki
112.229.119.202讨论2020年6月25日 (四) 13:17的版本 (创建页面,内容为“servlet+jsp+java实现Web 应用   用java来构建一个web应用是特别容易的事情,jsp和php很像,可以嵌套在html中。程序的结构很简单…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

servlet+jsp+java实现Web 应用

  用java来构建一个web应用是特别容易的事情,jsp和php很像,可以嵌套在html中。程序的结构很简单,也很清楚,本文主要记录下大概的开发过程和环境的搭建。

环境搭建

下载

   eclipse 
   tomcat
   eclipse tomcat 插件

开发过程

创建项目

1.建立一个Dynamic Web Project

项目名称:test03

创建一个欢迎页面

2.创建一个欢迎页面

页面可以是jsp/html,我们选择一个jsp页面(放在WebContent内)

<html>
<head>
<style type="text/css">
.layer2{width:400px; height:auto; padding:30px; margin:30px auto; background:#ddf; border-radius:8px;}

#login
  {  
  width:70%;margin:0 auto;
 
  }

#login, #login td
  {
     /*border:1px solid black; */
     border-collapse:collapse;
  }
 caption{
 	font:normal bold 150% "仿宋" ; color:#a00; text-align:center
 }
 #login td {
 	padding:10px;
 }
 #login input
  {
 
    height:35px;
 
  }
#login .col1{ width:35%;text-align:center;  }
#login .col2{ text-align:center; }
#login .col3{ text-align:center; height:45px;}

.bn{
	margin: 0 10px; font:normal bold 150% "宋体";
}
</style>
</head>
<body><div class="layer2">
<form  method="get" > 
<table id="login"> 
<caption>用户登录界面</caption>
<tr>
<td class="col1">用户名</td>
<td class="col2"><input type="text" name="uName"/></td>
</tr>

<tr>
<td class="col1">密码</td>
<td class="col2"><input type="password" name="pass" /></td>
</tr>

<tr>
<td colspan="2" class="col3"><input type="submit" value="提 交" class="bn"/>
                          <input type="reset" value="取 消" class="bn" /></td>

</tr>
</table>
</form>
</div>
</body>
</html>

创建servlet

2.向工程添加一个servlet文件:

新建一个package叫main,然后在包名 右键→New→Servlet

package com.example;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Welcome
 */
@WebServlet("/Welcome")
public class Welcome extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String c = request.getParameter("select");
        if(c.equals("yes"))
            out.print("Welcome!");
        else
            out.print("I don't like you!");
    }

}


.创建一个web.xml

web.xml用来建立servlet与jsp的关系(需要放在WEB-INF内)。

根据不同的url来调用不同的servlet来进行处理。


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name></display-name>
  <servlet>
    <servlet-name>Welcome</servlet-name>
    <servlet-class>demo.Welcome</servlet-class>
   </servlet>
   <servlet-mapping>
    <servlet-name>Welcome</servlet-name>
    <url-pattern>/hello.do</url-pattern>
  </servlet-mapping>
</web-app>


效果图:

Jw1-15.png

点击后:

Jw1-16.png

什么是MVC

MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写。其实上面的结构就是一种MVC,页面用jsp来展现,控制用servlet,而模型就是用普通的JAVA类来实现不同的处理过程。

参考:

https://www.cnblogs.com/coder2012/p/3389979.html