设计并实现一个接收客户端表单数据的Servlet

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Dian0dian0/article/details/83095702

目录

项目框架:

index.html文件内容:

First.java文件内容:

运行结果(背景终于选了个稍微合适的):

点击“登陆”结果:

总结:



实践作业3:自己独立设计并实现一个接收客户端表单数据的Servlet,从自己作业1所实现的表单中接收文本框和密码框中的内容并显示在生成的网页上(注意测试汉字乱码)。


项目框架:

index.html文件内容:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title><欢迎登陆中南民族大学信息门户</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="text/html; width=device-width,initial-scale=1.0; charset = gb2312">
        <style type="text/css">
         body{text-align:center }
         body{line-height:2}
         body {background:url("background.jpg"); background-repeat:no-repeat;background-size:  cover }
        </style>
          
    </head>
    <form action="First" method="POST" >
    <div> <p align="center" > <font size = "7" >欢迎登陆中南民族大学信息门户</font></p> </div>
        <br />
        <br />
        <br />
 姓名:
 <input style="width:150px;height:20px;background-color:orange" type="text" name="myName" />
     <br /> 
     <br />
 密码:
  <input style="width:150px;height:20px;background-color:orange;" type="password" name="pass"  >
     <br />
      <br />
  <input type="checkbox" name="radiobutton" value="radiobutton" checked> 记住密码? 
   <br />
    <br /> 
   <input type="submit" style="width:120px;height:40px;background-color:yellow;"  value="登陆">
   <input type="reset"  style="width:120px;height:40px;background-color:yellow;"  value="重置">
 <br />
<p>  
<!--<img src="70d346c20a27b293.jpg" alt="hahahaahahahaha"/>--> 
<br />
<a href ="http://www.scuec.edu.cn/jky/">中南民族大学计算机科学学院</a> 
</p> 

  
 </form>
    </body > 
</html>

在新建servlet时候一定要勾选那个小选项,生成.xml文件,真的是太重要了,我真的是因为这个东西恼火了一天。

First.java文件内容:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

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;

/**
 *
 * @author bestone
 */
public class First extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=gb2312");
        request.setCharacterEncoding("gb2312");
        

        PrintWriter out = response.getWriter();
        {
            String name = request.getParameter("myName");
            String password = request.getParameter("pass");
            /* TODO output your page here. You may use following sample code. */
           /* out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet First</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<L1><B>username</B>:"+
                    request.getParameter("myName")
                    +"<L1><B>password</B>:"+"\n"+
                    request.getParameter("pass"));
            
            out.println("</body>");
            out.println("</html>");*/
           out.println(
                   "<BODYBGCOLOR=\"#FDF5E6\">\n"+
                   "<H1 ALIGN-CENTER>"+"get post data"+"</H1>\n"+
                   "<UL>\n"+
                   "<L1><B>username</B>:"+
                    request.getParameter("myName")
                    +"<L1><B>password</B>:"+"\n"+
                    request.getParameter("pass")+
                    "</BODY></HTML>");
                    
            
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

运行结果(背景终于选了个稍微合适的):

点击“登陆”结果:

总结:

刚开始我安装netbeans IDE 并没有跑出个结果,卸载了重新安装就跑出来了,真的有点奇怪,感觉自己接触得也不多,但是可能这种需要设计,可能和我的爱好很接近,所以还是很感兴趣,老师真的是太负责了,居然每个作业都要检查。

在servlet中其实就是简单得修改了processRequest函数。理解了一下各个函数得作用吧~~还是很懵,我觉得我需要先把java书看完,从来没有试过,我试一次,看看效果。

猜你喜欢

转载自blog.csdn.net/Dian0dian0/article/details/83095702