JSP and servlet Simple login interface (no database attached)

Source: Internet
Author: User

1, first of all, although very simple, but for beginners this JSP and servlet link I've been tinkering with a half a day to figure out

The JSP submits the request to a URL, and the servlet gets the requested data from the URL, and the link is at that URL


Code in a JSP

File name: login.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "

pageencoding= "Utf-8"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>

<title> Login </title>

<body>

<form action= "http://localhost:80/loginServlet/LoginServlet" method= "POST" >

User: <input type= "text" name= "username" value= "wangmin"/><br/>

Password: <input type= "password" name= "password" value= "Wangmin"/><br/>

<input type= "Submit" value= "Login"/>

</form> AAAA

</body>

Code in a servlet

File name: Loginservlet.java

Package my.experience.login;


Import java.io.IOException;


Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;



public class Loginservlet extends httpservlet{

Overriding the Doget method

public void doget (HttpServletRequest request,

HttpServletResponse response)

Throws Servletexception,

IOException {

String username = request.getparameter ("username");

String Password = request.getparameter ("password");

Server-side printing information

System.out.println ("username=" + username);

System.out.println ("password=" + password);

Set the encoding format

Response.setcontenttype ("text/html;charset=gb18030");

Return to HTML page

Response.getwriter (). println ("

Response.getwriter (). println ("

Response.getwriter (). println ("<title> login Information </title>");

Response.getwriter (). println ("

Response.getwriter (). println ("<body>");

Response.getwriter (). println ("Welcome" "+ Username +" "User Login successful!!!" ");

Response.getwriter (). println ("</body>");

Response.getwriter (). println ("

}

Overriding the Dopost method

public void DoPost (HttpServletRequest request,

HttpServletResponse response)

Throws Servletexception,

IOException {

Doget (request, response);

}

}

Be sure to write the URL in Web. XML, otherwise you won't find

<?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 "id=" webapp_id "version=" 2.5 ">

<display-name>MyExperience</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<servlet>

<servlet-name>LoginServlet</servlet-name>

<servlet-class>my.experience.login.loginservlet</servlet-class>//Package name + class name

</servlet>

<servlet-mapping>

<servlet-name>LoginServlet</servlet-name>

<url-pattern>/loginServlet/LoginServlet</url-pattern> action in the corresponding JSP

</servlet-mapping>

</web-app>


Then Kai Tomacat, enter http://localhost:80/login.jsp in the browser OK

This article is from the "AutoComplete" blog, make sure to keep this source http://7129486.blog.51cto.com/7119486/1595806

JSP and servlet Simple login interface (no database attached)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.