无法将值形式的servlet转发给html [英] Unable to forward the values form servlet to html

查看:127
本文介绍了无法将值形式的servlet转发给html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将userName作为请求发送到html页面作为set属性,但是它并没有在FormDemo中显示消息,请让我知道我要去哪里。

 <!DOCTYPE html PUBLIC -  // W3C // DTD HTML 4.01 Transitional // EN 
http://www.w3.org/TR/html4/loose.dtd\">
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< title>登入表单< / title>
< / head>
< body>
< div align =center>
< form name =loginFormmethod =postaction =loginServlet>
用户名:< input type =textname =username/> <峰; br />
密码:< input type =passwordname =password/> <峰; br />
< input type =submitvalue =Login/>
< / form>
< / div>
< / body>
< / html>

登录servlet.java

  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 {
$ b / **
*
* /
private static final long serialVersionUID = 1L;
$ b保护void doPost(HttpServletRequest请求,
HttpServletResponse响应)抛出ServletException,IOException {

//读取表单字段
字符串username = request.getParameter (用户名);
String password = request.getParameter(password);

System.out.println(username:+ username);
System.out.println(password:+ password);
request.setAttribute(userName,username);
request.getRequestDispatcher(/ FormDemo.html)。forward(request,response);
}

}

FormDemo.html

 <!DOCTYPE html PUBLIC -  // W3C // DTD HTML 4.01 Transitional // EN
http:// www.w3.org/TR/html4/loose.dtd\">
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< title>表单演示< / title>
< / head>
< body>
< p> <%= request.getAttribute(userName)%>< / p>
< / body>
< / html>

Web.xml

 <?xml version =1.0encoding =UTF-8?> 
< web-app xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns =http://java.sun.com/xml/ns/javaeexsi :schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsdid =WebApp_IDversion =3.0 >
< display-name> LexisNexis< / display-name>
< welcome-file-list>
< welcome-file> index.html< / welcome-file>
< / welcome-file-list>

< servlet>
< servlet-name> LoginServlet< / servlet-name>
< servlet-class> package.LoginServlet< / servlet-class>
0< / load-on-startup>
< / servlet>

< servlet-mapping>
< servlet-name> LoginServlet< / servlet-name>
< url-pattern> / loginServlet< / url-pattern>
< / servlet-mapping>

< servlet>
< servlet-name> DemoServlet< / servlet-name>
< servlet-class> package.DemoServlet< / servlet-class>
< / servlet>

< servlet-mapping>
< servlet-name> DemoServlet< / servlet-name>
< url-pattern> / demoServlet< / url-pattern>
< / servlet-mapping>

< / web-app>


解决方案

没有办法将这样的值写入一个普通的HTML页面。您需要使用 JSP 来执行此操作。


I am trying to Send userName as request to html page as set attribute, But it is not displaying the msgs in FormDemo, Please let me know where i am going wrong.

Login page

<!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=UTF-8">
<title>Login Form</title>
</head>
<body>
<div align="center">
<form name="loginForm" method="post" action="loginServlet">
    Username: <input type="text" name="username"/> <br/>
    Password: <input type="password" name="password"/> <br/>
    <input type="submit" value="Login" />
</form>
</div>
</body>
</html>

Login servlet.java

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;

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

    // read form fields
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    System.out.println("username: " + username);
    System.out.println("password: " + password);
    request.setAttribute("userName", username);
         request.getRequestDispatcher("/FormDemo.html").forward(request,response);
}

}

FormDemo.html

   <!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=UTF-8">
  <title>Form Demo</title>
   </head>
   <body>
   <p> <%= request.getAttribute("userName") %></p>
   </body>
  </html>

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" 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>LexisNexis</display-name>
       <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        </welcome-file-list>

           <servlet>
          <servlet-name>LoginServlet</servlet-name>
             <servlet-class>package.LoginServlet</servlet-class>
           <load-on-startup>0</load-on-startup>
           </servlet>

           <servlet-mapping>
          <servlet-name>LoginServlet</servlet-name>
             <url-pattern>/loginServlet</url-pattern>
             </servlet-mapping>

          <servlet>
          <servlet-name>DemoServlet</servlet-name>
               <servlet-class>package.DemoServlet</servlet-class>
           </servlet>

          <servlet-mapping>
               <servlet-name>DemoServlet</servlet-name>
               <url-pattern>/demoServlet</url-pattern>
             </servlet-mapping>

            </web-app>

解决方案

There is not a way to write values like this into a plain HTML page. You will need to use a JSP to do this.

这篇关于无法将值形式的servlet转发给html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆