Android studio 032 java Tomcat Servlet 搭建服务器

第一步新建D web  project
 
第二步新建servlet


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

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 TestPage
 */
@WebServlet("/TestPage")
public class TestPage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestPage() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
//        PrintWriter out = response.getWriter();
//        Date now = new Date(); // Date & Time
//        String page = "

" + now +"

";
// out.println(page); String username = request.getParameter("username"); String password = request.getParameter("password"); response.setContentType("text/html;charset=utf-8"); // 浏览器默认的编码是iso-8859-1转换成utf-8,如果不转换,会产生乱码。 String name = new String(username.getBytes("iso-8859-1"), "utf-8"); String pass = new String(password.getBytes("iso-8859-1"), "utf-8"); System.out.println("username" + name); System.out.println("password" + pass); //如果登陆名是张三,密码是123,登陆成功,否则登陆失败。 if ("abc".equals(name)&&pass.equals("123")) { response.getWriter().write("登陆成功"); } else response.getWriter().write("用户名或密码错误"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }

新建web.xlm


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>TestPagedisplay-name>
  
  <servlet>
        <servlet-name>TestPageservlet-name>
        <servlet-class>TestPageservlet-class>
    servlet>

    <servlet-mapping>
        <servlet-name>TestPageservlet-name>
        <url-pattern>/TestPageurl-pattern>
    servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
web-app>

新建index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
    This is my JSP page.
    <br>
    <form action="TestPage" method="get">
        用户名:<input type="text" id="username" name="username" /> <br>
        密码: <input type="text" id="password" name="password" /> <br> <input
            type="submit" value="提交" />

    form>
body>
html>

运行
Android studio 032 java Tomcat Servlet 搭建服务器_第1张图片

你可能感兴趣的:(Androidstudio,Android,studio,代码)