JSP 说helloworld

这是我学习JSp的helloworld

由于初学就不说原理了,直接来上代码!

1整体架构如下:


2 user.java代码

package com.syw;

public class User {

	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}

3 userserlevt.java 代码

package com.syw;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.jersey.api.core.HttpResponseContext;
import com.sun.jersey.spi.dispatch.RequestDispatcher;

public class Userservlet extends HttpServlet {

	protected void doget(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException
	{
		doPost(req, resp);
		String username = req.getParameter("username");
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException
	{
		String usname = req.getParameter("username"); //取得输入信息
		User user = new User();
		user.setUsername(usname);
		req.setAttribute("user", user); //将对象添加到HttpServletRequest 对象中
		javax.servlet.RequestDispatcher rdt =  req.getRequestDispatcher("/index.jsp");//将请求转向指定页面
		((javax.servlet.RequestDispatcher) rdt).forward(req, resp);
	}
	
}

4 index.jsp

<%@ page contentType="text/html; charset=gb2312"%>
<%@ page language="java" import="java.util.*,com.syw.*"
	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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<base href="<%=basePath%>">
<title>load</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">
	-->
<script language="javaScript">
	//首先判断输入的是否是空值,不是则提交
	function checkstr() {
		if (document.getElementById("CustomerID").value == "") {
			alert("用户名不能为空!");
			return false;
		}
		if (document.getElementById("CompanyName").value == "") {
			alert("密码不能为空!");
			return false;
		}
		form.submit();
	}
</script>
</head>

<body>
	<%
		User user = (User) request.getAttribute("user");
		if (user != null && user.getUsername() != null) {
			if (user.getUsername().equals("")) {
				out.println("请输入一个用户名");
			} else {
	%>
	你好:<%=user.getUsername()%>
	<%
		}
		}
	%>
	<form action="index" method="post"> 用户名:<input type="text"
		name="username" value=""> <br>
	<input type="submit" name="submit" value="submit" onclick="checkstr()">
	</form>
</body>
</html>

5 wed.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_3_0.xsd" version="3.0">
  <display-name>TEST</display-name>
  <servlet>
    <description>JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>userservlet</servlet-name>
    <servlet-class>com.syw.Userservlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>userservlet</servlet-name>
    <url-pattern>/index</url-pattern>
  </servlet-mapping>
</web-app>

OK 运行效果:




主要代码给出来了哈!什么配置tomcat jdk 就不要问我了,还是google吧!

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。