JavaWeb学习记录

大学JavaWeb课程学习记录

简单的记录一下学习成果
方便之后期末的作业啦

第一课

安装软件并配置环境

  • eclipse

    采用版本2019-06,老师直接提供的,我也就不改了。不要问为什么不用IDEA,问就是环境配置和接下来的学习会有不同,不利于我们这样的新手😎

  • tomcat

    采用版本8.5.57

  • mysql

经过本人惨痛的教训,该版本的eclipse不适用过高版本的java,本人之后采用的皆为Java1.8

tomcat下载完后找到".\tomcat\bin\Tomcat_8w.exe"点击start启动,点击stop关闭。
在浏览器输入127.0.0.1:8080如果进入tomcat页面即表示启动成功。

打开eclipse

  1. 点击window->Preferences->General->Workspace->Text file encoding->Other选择UTF-8
    同时window->Preferences->Web->JSP Files->Encoding选择ISO 10646/Unicode(UTF-8)此举可以令后续的jsp页面显示中文。

  2. 点击window->Preferences->Server->Runtime Environments点击Add添加tomcat版本。

博主选择Apache Tomcat v8.5

随后 next 在Tomcat installation directory选择tomcat的文件夹,最后Finish

选择文件名为tomcat即可

  1. 点击window->Preferences->Web Server->Server and Runtime在Server runtime选择自己的tomcat版本.

例如Tomcat v8.5 Server,最后点击Apply and Close

  1. 新建新Dynamic Web Project文件,在Target runtime选择自己的tomcat版本,同上。命名后Finish。

  2. 在编译器界面的下方找到Servers,点击并双击显示的英文,跳转Overview页面在Ports选项下改变两个端口。

博主为81238023

就此,配置已经完成了!

第一个程序

打开我们之前创建的文件.

  1. 右键点击[JAVA Resources]下的[src],新建Servlet文件并命名,这便是动态网页交互的灵魂,java代码也是写在其中。
    一个简单的登录逻辑
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    package action;

    import java.io.IOException;
    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 Class02
    */
    @WebServlet("/Class02")
    public class Class02 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * @see HttpServlet#HttpServlet()
    */
    public Class02() {
    super();
    // TODO Auto-generated constructor stub
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //1.获取用户名和密码
    //2.判断信息是否正确,并跳转
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    System.out.println("username:"+username);
    if(username.equals("lhj") && password.equals("123")) {
    response.sendRedirect("welcome.jsp");
    }
    else {
    response.sendRedirect("failed.jsp");
    }
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    }

    注意是逻辑是写在
    1
    2
    3
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //write
    }
  1. 接下来我们来建立页面,右键点击[WebContent]选择新建jsp文件
    一个简单的登录页

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>登录页面</title>
    </head>
    <body>
    <form action="Class02" method="get">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value="登录">
    </form>
    </body>
    </html>

    在html中我们学过actionmethod的用法,所以

    1
    <form action="[你的Servrlt包名]" method="get">
  2. 最后右键该登录界面选择Run asRun on Server后直接Finish即可。