zhuhb1234 2016-08-04 08:58 采纳率: 50%
浏览 1579
已采纳

各位,在线等 servlet跳不进!!!!!

web.xml

 <?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>test2.5</display-name>
    <!-- <listener>
        <listener-class>javax.servlet.ServletContextListener</listener-class>
    </listener>
    <listener>
        <listener-class>javax.servlet.http.HttpSessionListener</listener-class>
    </listener> -->
    <!-- <filter>
        <filter-name>encodingfilter</filter-name>
        <filter-class>com.hr.test.filter.EncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingfilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>-->

    <servlet>
        <servlet-name>loginServlet</servlet-name>
        <servlet-class>com.hr.test.servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>loginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

servlet

 package com.hr.test.servlet;

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;

public class LoginServlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void init() throws ServletException {
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response){

        String name = (String) request.getParameter("name");
        String pwd = (String) request.getParameter("pwd");

        if (name.equals("1234") && pwd.equals("123")) {
            response.setContentType("text/html");
            PrintWriter out=null;
            try {
                out = response.getWriter();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            out.print("登陆成功!!!");
        }
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }
}

index.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>

<body>
    <form action="http://localhost:8080/loginServlet" method="post">
        <table>
            <tr>
                <th>用户名</th>
                <th><input name="name"/></th>
            </tr>
            <tr>
                <th>密码</th>
                <th><input name="pwd"/></th>
            </tr>
            <tr>
                <th rowspan="1"></th>
                <th><input value="登陆" type="submit"/></th>
            </tr>
        </table>
    </form>
</body>
</html>

  • 写回答

4条回答 默认 最新

  • Jsvea 2016-08-04 09:04
    关注

    一:你先把Servlet中的PrintWrite释放掉,out.close();
    二:表单的action地址改成LoginServlet就行

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码