课程作业

1.假设有如下数据表:

create table Blog(

blogId int auto_increment primary key comment "博客Id",  

blogTitle varchar(10) comment "博客标题",

blogContent varchar(100) comment "博客内容" )

ENGINE=InnoDB  DEFAULT CHARSET=utf8;


提供一个发表博客的接口,可以通过POST方法(表单)提交两个参数,blogTitle,blogContent,类型为String,并且限制blogTitle长度20个英文字符,blogContent长度为100个英文字符(假设客户端传过来的内容都为英文字符),如果内容不符合长度要求,向客户端返回400响应码,如果符合长度要求,则将内容保存到Blog数据表中,并返回200响应码给客户端。

注:如果不熟悉HTML表单,可以参考http://www.w3school.com.cn/tags/tag_form.asp 。

答案内容超过5000字建议使用附件形式上传

接口

dao

</pre><pre name="code" class="java">package com.netease.course.controller;
public interface BlogDAO {
    public void insertArticle(String title,String content);
    public int count();
}
service
package com.netease.course.controller;

public interface BlogService {
    public void pressblog(String title,String content);
}
测试

package com.netease.course.controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/into")
public class Test {
    @RequestMapping("/blogger")
    @ResponseBody
    public String update(@RequestParam String blogTitle,@RequestParam String blogContent,HttpServletResponse resp<span style="font-family: Arial, Helvetica, sans-serif;">){</span>
        ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
        BlogService blogservice = context.getBean("blogservice",BlogService.class);


        if(blogTitle.length()<21&&blogContent.length()<101&&blogTitle.length()>0&&blogContent.length()>0)//判断输入合法性
        {
            blogservice.pressblog(blogTitle,blogContent);
            ((ConfigurableApplicationContext)context).close();
            return "200,ok.blog insert success.<br>Please open developer tools to check statuscode.";}
        else {
            ((ConfigurableApplicationContext)context).close();
            return "blog insert failed.<br>Please open developer tools to check statuscode.<br>\nCheck the size of Title or Content.<br><h1>BlogTitle:"
<span style="white-space:pre">		</span>+blogTitle;}

    }

配置文件。

application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

	xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       ">

	<context:component-scan base-package="com.netease.course" />
	<context:annotation-config />

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/example"></property>
		<property name="username" value="root"></property>
		<property name="password" value="mypwd123"></property>

	</bean>
	<bean id="blogdao" class="com.netease.course.controller.BlogDAOImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<bean id="blogservice" class="com.netease.course.controller.BlogServiceImpl">
		<property name="blog" ref="blogdao"></property>
	</bean>



</beans>

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" version="3.0">
  <display-name>blogsystem</display-name>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:application-context.xml </param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>example</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>example</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
index.jsp
<html>
<body>
<h2>Hello World!</h2>
<p>endBlogTest</p>
<a href="./insertArticle-login.html">To Blogger</a>
</body>
</html>
表格输入。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Demo</title>
</head>
<body>
    <p>BlogTest</p>
    <form action="api/into/blogger" method="post">
        <p>Title <input type="text" name="blogTitle" /></p>
        <p>Content: <input type="text" name="blogContent"/></p>
        <input type="submit" value="Submit" />
    </form>

</body>
</html>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.netease.course</groupId>
  <artifactId>blogsystem</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>
            UTF-8
        </project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <encoding>utf-8</encoding>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
example-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.netease.course" />
    <mvc:annotation-driven></mvc:annotation-driven>
    
</beans>
输入不符时,

错误


结果
数据表没有变化

正确


返回



文章已经插入。



代码包:https://github.com/sofapa/blogsystem/archive/master.zip


2.

完成一个具有登陆功能的简单Web工程,要求:

1. 提供一个简单的登陆表单,用户可以输入用户名(userName),密码(userPassword),点击提交后登陆;

2. 后端提供一个登陆接口(login),可以判断用户是否是合法用户(从创建好的用户表中查询数据,可预先在数据库中插入几条数据);

3. 登陆成功后,显示用户信息页面(user),内容自定义,如显示用户名,并给出一个欢迎消息;

4. 如果未登陆用户直接访问用户信息页面(user),向客户端返回一个错误页面(error),内容自定义,如提示用户未登陆等。


注:

1. 建表及插入语句

create table User(

userId int auto_increment primary key comment "用户Id",  

userName varchar(100) comment "用户名称",

userPassword varchar(100) comment "用户密码" )

ENGINE=InnoDB  DEFAULT CHARSET=utf8;


insert into User (userName, userPassword) values ("test1", "test1")

insert into User (userName, userPassword) values ("test2", "test2")

上述语句插入两个用户test1及test2,密码分别为test1,test2


2. 需要用到cookie及session相关内容来检查当前请求是否为登陆用户的请求;


答案内容超过5000字建议使用附件形式上传


地址:https://code.csdn.net/sinoacc/web-login/repository/archive?ref=master

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值