使用servlet實(shí)現(xiàn)一個(gè)用戶登錄功能

這篇文章給大家介紹使用servlet實(shí)現(xiàn)一個(gè)用戶登錄功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

我們提供的服務(wù)有:成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、金沙ssl等。為上千余家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的金沙網(wǎng)站制作公司

用servlet來(lái)簡(jiǎn)單實(shí)現(xiàn)一個(gè)用戶登錄的小程序。
首先,servlet也是一個(gè)JAVA類,新建一個(gè)JAVA類,它直接繼承了HttpServlet類:

@WebServlet(name = "ServletTest")
public class ServletTest extends HttpServlet {

  @Override
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }

  @Override
  public void init() throws ServletException {
    super.init();
  }

  @Override
  public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    //super.service(req, res);
    String username=req.getParameter("username");
    String password=req.getParameter("password");
    System.out.println(username);
    System.out.println(password);

  }

  @Override
  public void destroy() {
    super.destroy();
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    super.doPost(req, resp);
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    super.doGet(req, resp);
  }
}

Servlet作為一個(gè)程序,也會(huì)有它的生命周期,它的生命周期從init()開(kāi)始,有兩種init(),一個(gè)是有參數(shù)的,另一個(gè)是無(wú)參數(shù)的。其中有參數(shù)的init方法會(huì)顯示地調(diào)用無(wú)參的init()方法。從init(ServletConfi,g config)的源代碼中我們也可以看出這一點(diǎn)。init()是在被Servlet被訪問(wèn)時(shí)被調(diào)用

使用servlet實(shí)現(xiàn)一個(gè)用戶登錄功能

當(dāng)servlet接受用戶提交的表單的時(shí)候會(huì)調(diào)用service方法。

然后編寫(xiě)login.jsp:

<form action="<%= request.getContextPath() %>/loginServlet" method="post">
  用戶名:<input type="text" name="username"><br/>
  密碼:<input type="password" name="password"><br/>
  <input type="submit" value="登錄">
  <input type="reset" value="重置">
</form>

注意action中要使用request.getContextPath獲取到上下文路徑。
loginServlet需要在web.xml中進(jìn)行配置

<servlet>
       <servlet-name>LoginServlet</servlet-name>
       <servlet-class>com.myServlet.ServletTest</servlet-class>
     </servlet>

     <servlet-mapping>
       <servlet-name>LoginServlet</servlet-name>
       <url-pattern>/loginServlet</url-pattern>
     </servlet-mapping>

servlet-name隨便起,servlet-class表示servlet的類名。
url-pattern是action中引用的名字,這樣它們就關(guān)聯(lián)在一起了。

最后就可以看見(jiàn)表單中的username和password被打印出來(lái)了。

使用servlet實(shí)現(xiàn)一個(gè)用戶登錄功能

關(guān)于使用servlet實(shí)現(xiàn)一個(gè)用戶登錄功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

新聞標(biāo)題:使用servlet實(shí)現(xiàn)一個(gè)用戶登錄功能
本文來(lái)源:http://bm7419.com/article40/pcgeho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、微信公眾號(hào)、定制網(wǎng)站、做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開(kāi)發(fā)公司