关闭

GWT整合JSP案例

653人阅读 评论(0) 收藏 举报
分类:


目录结构:

目录

以下是源码:

package com.app.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Test implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		HorizontalPanel horPanel = new HorizontalPanel();
		final TextBox titleBox = new TextBox();
		Button btn = new Button("Send");
		btn.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				String protocol = Window.Location.getProtocol();
				String host = Window.Location.getHost();
				Window.open(protocol + "//" + host + "/test/print?text="+titleBox.getValue(), "print", null);
			}
		});
		String protocol = Window.Location.getProtocol();
		String host = Window.Location.getHost();
		System.out.println(protocol);
		System.out.println(host);
		horPanel.add(titleBox);
		horPanel.add(btn);
		RootPanel.get().add(horPanel);
	}
}

package com.app.print;

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 PrintServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		req.setAttribute("text", req.getParameter("text"));
		req.getRequestDispatcher("/print.jsp").forward(req, resp);
	}

}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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">
<style type="text/css">
</style>
</head>
<body>

	<h1>Text: <%= request.getAttribute("text") %> </h1>
</body>
</html>

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee">

  <!-- Servlets -->
  <servlet>
    <servlet-name>printservlet</servlet-name>
    <servlet-class>com.app.print.PrintServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>printservlet</servlet-name>
    <url-pattern>/test/print</url-pattern>
  </servlet-mapping>
  
  <!-- Default page to serve -->
  <welcome-file-list>
    <welcome-file>Test.html</welcome-file>
  </welcome-file-list>

</web-app>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:37124次
    • 积分:680
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:39篇
    • 译文:2篇
    • 评论:1条
    文章分类