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" οnclick="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吧!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值