m0_37173810
零薄狱
2017-08-21 04:39

Intellij idea2017怎么配置urlpatterns

1
  • servlet
  • xml
  • intellij-idea

这是网页代码

<%--
  Created by IntelliJ IDEA.
  User: Limbo
  Date: 2017/8/21
  Time: 9:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/SV" method="post">
    输入姓名<input type="text" id="username">
    输入密码<input type="password" name="" id="userpsd">
    <input type="submit" value="提交">
</form>

</body>
</html>

这是Servlet代码

 package com.limbo.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "MyServlet",urlPatterns = "/SV")
public class MyServlet extends HttpServlet {

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("这是Servlet");
        response.setContentType("text/htmml;charset=utf-8");
//        PrintWriter out = response.getWriter();
//                out.println(",欢迎登陆成功</h2>");
//        response.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String psd = request.getParameter("userpsd");
        if (username != null && !"".equals(username) && psd != null && !"".equals(psd)) {
            if (username.equals("admin") && psd.equals("123")) {
                PrintWriter out = response.getWriter();
                out.print("<h2>" + username);
                out.println(",欢迎登陆成功</h2>");

            } else {
//                response.sendRedirect("index.jsp");
                PrintWriter out = response.getWriter();
                out.print("<h2>" + username);
                out.println(",欢迎登陆成功</h2>");
            }


        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}

输入名字密码后就是空白页面,地址栏地址会变成localhost:8080/SV

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答