简体   繁体   English

JSP页面在Servlet程序中不起作用

[英]JSP page not working in Servlet Program

Actually i'm trying to display the details obtained from JSP form with servlet. 实际上,我正在尝试显示从JSP表单使用servlet获得的详细信息。 But I'm not able to display the JSP page. 但是我无法显示JSP页面。 But I can see the program entering into the POST method in Servlet. 但是我可以看到程序进入Servlet中的POST方法。

Here is my code, 这是我的代码,

Startup.jsp Startup.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <form action="controlServlets" method="post">
        <input type="text" name="name"/><br>        
        <input type="text" name="group"/>
        <input type="text" name="pass"/>
        <input type="submit" value="submit">            
    </form>

</body>
</html>

web.xml web.xml

<web-app>

  <servlet>
    <servlet-name>controlServlets</servlet-name>
    <servlet-class>com.selenium8x8.servlet.ControlServlets</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>controlServlets</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

</web-app>

ControlServlets.java ControlServlets.java

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ControlServlets")
public class ControlServlets extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public ControlServlets() {
        super();
        // TODO Auto-generated constructor stub
    }


//    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);  
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            String name = request.getParameter("name");
            String group = request.getParameter("group");
            String pass = request.getParameter("pass");
            System.out.println("Name :"+ name);
            System.out.println("group :"+ group);
            System.out.println("pass :"+ pass);
            System.out.println("Post method");
    }

}

In console, 在控制台中

I can see the following, 我可以看到以下内容

Name :null
group :null
pass :null
Post method

Please Help... 请帮忙...

Part I)If you want to use web.xml for your application then you need to make following changes : 第一部分)如果要为应用程序使用web.xml ,则需要进行以下更改:

1)In Startup.jsp change the action attribute of <form> tag to 1)在Startup.jsp<form>标记的action属性更改为

<form action="ControlServlets" method="post"> 
              ↑

2)In web.xml change the <servlet-mapping> to 2)在web.xml<servlet-mapping>更改为

<servlet-mapping>
 <servlet-name>controlServlets</servlet-name>
 <url-pattern>/ControlServlets</url-pattern> 
</servlet-mapping>  

3)In ControlServlets.java several changes as, in web.xml you mentioned 3)在ControlServlets.java中,您提到了web.xml一些更改

<servlet-class>com.selenium8x8.servlet.ControlServlets</servlet-class>
                ↑

This is the package name, so you must have first statement in ControlServlets.java 这是程序包名称,因此您必须在ControlServlets.java具有第一条语句

package com.selenium8x8.servlet;  //in your code it is missing  

Then, comment following two lines 然后,评论以下两行

//import javax.servlet.annotation.WebServlet;

and

//@WebServlet("/ControlServlets")

Now, run application, it will give you desired output. 现在,运行应用程序,它将为您提供所需的输出。


Part II) If you want to use @WebServlet annotation, as you did 第二部分)如果您想像使用@WebServlet注释那样

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ControlServlets")
public class ControlServlets extends HttpServlet {   
  ...
  .....
  .......
} 

Then, no need for web.xml . 然后,不需要web.xml The above does basically the same as following: 上面的内容基本上与以下内容相同:

<servlet>
 <servlet-name>controlServlets</servlet-name>
 <servlet-class>com.selenium8x8.servlet.ControlServlets</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>controlServlets</servlet-name>
 <url-pattern>/ControlServlets</url-pattern>
</servlet-mapping>  

For using @WebServlet annotation you need Java EE 6 / Servlet 3.0 为了使用@WebServlet批注,您需要Java EE 6 / Servlet 3.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM