一个简单的Java Web项目

一、效果

一个简单的Java Web例子:
web界面:一个简单的Java Web项目_第1张图片
登录成功:一个简单的Java Web项目_第2张图片
登录失败:这里写图片描述

二、过程(jsp+Servlet)

1、创建一个默认的Web Application项目,则默认生成:index.jsp,web.xml等文件
一个简单的Java Web项目_第3张图片
2、编辑index.jsp代码,使用表单实现登录界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Millettitle>
head>
<body>
<form action="LoginServlet.action" method="post">
    <table>
        <tr>
            <td>用户名:td>
            <td><input type="text" name="username">td>
        tr>
        <tr>
            <td>密码:td>
            <td><input type="password" name="password">td>
        tr>
        <tr>
            <td>
                <input type="submit" value="登录">
            td>
            <td>
                <input type="reset" value="取消">
            td>
        tr>
    table>
form>
body>
html>

重点是:action属性的设置,method请求方法的设置
3、编写Servlet代码
LoginServlet.java代码

public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String _userName = req.getParameter("username");
        String _passWord = req.getParameter("password");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter _printWriter = resp.getWriter();
        _printWriter.println("");
        if ("admin".equals(_userName) && "admin".equals(_passWord)) {
            _printWriter.println("登录成功!欢迎您," + _userName);
        } else {
            _printWriter.println("对不起!您的用户名或者密码不正确!");
        }
        _printWriter.println("");
    }

}

4、在web.xml中配置jsp和servlet关系


<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <welcome-file-list>
        <welcome-file>index.jspwelcome-file>
    welcome-file-list>
    <servlet>
        
        <servlet-name>LoginServletservlet-name>
        
        <servlet-class>millet.LoginServletservlet-class>
    servlet>
    <servlet-mapping>
        
        <servlet-name>LoginServletservlet-name>
        
        <url-pattern>/LoginServlet.actionurl-pattern>
    servlet-mapping>
web-app>

5、注意,在Serlvet3.0新特性注解@WebServlet

@WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet.action"})
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String _userName = req.getParameter("username");
        String _passWord = req.getParameter("password");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter _printWriter = resp.getWriter();
        _printWriter.println("");
        if ("admin".equals(_userName) && "admin".equals(_passWord)) {
            _printWriter.println("登录成功!欢迎您," + _userName);
        } else {
            _printWriter.println("对不起!您的用户名或者密码不正确!");
        }
        _printWriter.println("");
    }

}

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