我想使用servlet创建一个简单的网页。这是我考试的一个问题,我考试时考试成功,但是没有按照他们的预期进行,但现在我想澄清一下我的问题。 我的问题是有一个网页,它分为两部分。在底部,它应显示文本框和文本区域以输入注释。还有一个提交按钮。当提交按钮点击时,输入的评论应显示在同一页面的顶部。 为此,我创建了一个servlet调用Welcome.java,并使用iframe将其分为两部分。然后我创建了一个servlet调用test.java并在该servlet中创建了一个文本框和textarea。然后创建另一个servlet调用text22.java来捕获注释并显示它们。但它没有给出预期的产量 我附上了这个页面现在看起来的截图,我想在页面顶部显示评论。请帮我解决这个问题。非常感谢你的帮助...
Welcome.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;
/**
*
* @author neil
*/
public class Welcome extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<iframe src='test22' name='if1' width='100%' height='400px'>");
out.println("</iframe>");
out.println("<iframe src='test' name='if1' width='100%' height='200px'>");
out.println("</iframe>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
test.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author neil
*/
public class test extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("User Name" + "<input type='text' name='username'/>" + "<br>" + "<br>");
out.println("<textarea name='comment' rows='25' cols='20'>" + "Write your comment");
out.println("</textarea>");
out.println("<input type='submit' value='Submit'/>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
test22.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author neil
*/
public class A extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
ServletContext sc=request.getServletContext();
String com= (String) sc.getAttribute("text");
if(null==com){
com = request.getParameter("comment");
}else{
com=com+"<br>"+request.getParameter("comment");
}
out.println(com);
sc.setAttribute("comment", com);
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
答案 0 :(得分:0)
您不必使这个简单的servlet程序复杂化。您需要有一个jsp
和servlet
才能执行此操作。
您的要求是使用您首先提交数据并查看的相同页面。
首先使用textbox
创建简单的jsp页面。您应该使用JSTL
代替scriptlets
,为了简单起见,我已经为您提供了scriptlet代码。
您的index.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>Submit and View Page</title>
</head>
<body>
<!-- This part will enable only after submit your username -->
<%
if(null != request.getParameter("username")){
out.println("<fieldset><legend>Entered Name</legend>");
out.println(request.getParameter("username"));
out.println("</fieldset>");
}
%>
<!-- End of view data -->
<form action="HelloServlet">
<label>User Name: </label><input type='text' name='username'/></br>
<input type='submit' value='Submit'/>
</form>
</body>
</html>
创建相应的servlet说HelloServlet
,
package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// Receive the username
String username = req.getParameter("username");
// Set it into request object
req.setAttribute("username", username);
// Forward it into same index page
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
配置部署描述符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>Sample_Servlet</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
确保您的文件夹结构如下所示,您也可以从教程中学习。
答案 1 :(得分:0)
Ajax和Javascript可用于更新页面内容而无需刷新整个页面。
开发此应用程序的方法如下:
只需要一个网页,其中包含将页面分成两部分的代码。它应在第二部分显示文本框,文本区域和按钮。并在第一部分中显示div(以显示注释)。此页面还应包含单击按钮时要处理的代码。单击按钮时,应启动Ajax调用。这个ajax调用应该通过传递输入的注释来命中servlet。
servlet应该处理注释并将注释作为响应返回。通过编写javascript代码,可以将返回的注释添加到页面的第一部分。